0

im currently stuck with this crash, I tried to fixed looking for an answer around this comunity but still no working for me. Can somebody please help me?

this is my code

public GameObject GetPooledObject()
    {
        for (int i = 0; i < pooledObjects.Count; i++)
        {
            if (!pooledObjects[i].activeInHierarchy)
            {
                return pooledObjects[i];
            }


        }

        GameObject obj = (GameObject)Instantiate(pooledObject);
        obj.SetActive(false);
        pooledObjects.Add(obj);

    }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 3
    You need to add `return obj` at the end. – Mahesh Oct 12 '18 at 01:36
  • 1
    What happens if your for loop ends without finding an object? – Retired Ninja Oct 12 '18 at 01:37
  • 3
    Note that "crash" means that the application stops unexpectedly. In its current state, your code won't compile, let alone crash. – ProgrammingLlama Oct 12 '18 at 01:46
  • 1
    I'm going to assume that you meant to say that you get a compile error. When seeking help with a compile error, it is customary to say what that error is. A crash is when your program has serious problems and stops working when you run it. – Jonathan Wood Oct 12 '18 at 02:17
  • Possible duplicate of [C# error: not all code paths return a value](https://stackoverflow.com/questions/19903404/c-sharp-error-not-all-code-paths-return-a-value) – Richardissimo Oct 12 '18 at 04:58

2 Answers2

0

Below

pooledObjects.Add(obj);

add

return null;

That way the entire method returns no matter what

kennyzx
  • 12,845
  • 6
  • 39
  • 83
China Syndrome
  • 953
  • 12
  • 24
  • 3
    Not necessarily return null. It can also return an instance. So “add a return statement” would be more acceptable than “add return null”. – kennyzx Oct 12 '18 at 03:57
0

Because as per your method design, you need to return something.

Janmejay Kumar
  • 309
  • 2
  • 7