0

im working at a infinit runner game and i want to add objectpooling to the platforms, but i got 2 errors:

Assets\PlatformGenerator.cs(37,26): error CS1501: No overload for method 'SpawnObject' takes 1 arguments
Assets\ObjectPool.cs(36,16): error CS0165: Use of unassigned local variable 'ToReturn'

Here is the platform generator script:

{
public GameObject ThePlatform;
public Transform GenerationPoint;
public float DistanceBetween;

private float PlatFormWidth;

public float DistanceBetweenMin;

public float DistanceBetweenMax;

public ObjectPool PlatformPool;

// Start is called before the first frame update
void Start()
{
    PlatFormWidth = ThePlatform.GetComponent<BoxCollider2D>().size.x;
    
}

// Update is called once per frame
void Update()
{
    if(transform.position.x < GenerationPoint.position.x)
    {
        DistanceBetween = Random.Range(DistanceBetweenMin, DistanceBetweenMax);

        transform.position = new Vector3(transform.position.x + PlatFormWidth + DistanceBetween, 
transform.position.y, transform.position.z);

        //Instantiate(ThePlatform, transform.position, transform.rotation);
        
        PlatformPool.SpawnObject(ThePlatform, transform.position, transform.rotation);

    }
    
}
}

Here is the ObjectPooler script:

{

public GameObject ObjectToPool;

public List<GameObject> ThePool = new List<GameObject>();

public int StartAmount;

// Start is called before the first frame update
void Start()
{
    for(int i = 0; i < StartAmount; i++)
    {
        ThePool.Add(Instantiate(ObjectToPool));
        ThePool[i].SetActive(false);
        ThePool[i].transform.parent = transform;
    }
}

// Update is called once per frame
void Update()
{
    
}

public GameObject SpawnObject(Vector3 Position)
{
    GameObject ToReturn;

    ToReturn = ThePool[0];
    ThePool.RemoveAt(0);

    ToReturn.transform.position = Position;

    ToReturn.SetActive(true);
   
    return ToReturn;
}
}

hope you can help me because im new at programming and don't now hot fix things like this and if you see other things i should improve it would be very nice if you tell it me.

2 Answers2

0

In the Object Pooler script you put the following function public GameObject SpawnObject(Vector3 Position) which takes only one argument for position. But in the platform generator you are calling the function with three arguments PlatformPool.SpawnObject(ThePlatform, transform.position, transform.rotation). So, replace the PlatformPool.SpawnObject(ThePlatform, transform.position, transform.rotation) in the platform generator script with PlatformPool.SpawnObject(transform.position);.

Easwar Chinraj
  • 436
  • 1
  • 4
  • 10
  • Thank you, you fixed the error about to much arguments, but i got a new error: `NullReferenceException: Object reference not set to an instance of an object PlatformGenerator.Update () (at Assets/PlatformGenerator.cs:37)` – FaultyDaantje Sep 25 '20 at 15:49
0

First error, your method is only asking for one argument. You are passing 3. (Vector3 just represents the x,y,z points of a transform or a gameObject.) If you wanna dynamically pass a Vector3 object, you can do something like this

Vector3 v = new Vector3 (0,0,0);

then you pass it on to your method SpawnObject(v);

For the second error, make sure ThePool list is not empty and you are assigning them properly, because you are assigning it to ToReturn.

Logging helps a lot, try to debug something like this

Debug.LogError(ThePool == null);

or check it's length

Debug.LogError(ThePool.Length);

if it is, try to add a condition so that you don't get an error

if(ToReturn != null)
// ....
Sophie
  • 21
  • 6
  • thank you, you fixed the error, but now i got a new error: `NullReferenceException: Object reference not set to an instance of an object PlatformGenerator.Update () (at Assets/PlatformGenerator.cs:37)` – FaultyDaantje Sep 25 '20 at 15:51
  • The gameObject you're trying to use in line 37 is null. Make sure you instantiate and assign it. – Sophie Sep 25 '20 at 16:01
  • hello, i am still learning coding and i don't know what i have to replace, can you tell me? Here is the part of the code that got the error: `public GameObject SpawnObject(Vector3 Position) { Debug.LogError(ThePool == null); GameObject ToReturn; ToReturn = ThePool[0]; ThePool.RemoveAt(0); ToReturn.transform.position = Position; ToReturn.SetActive(true); return ToReturn; }` – FaultyDaantje Sep 28 '20 at 15:08