0
\$\begingroup\$

I have two scenes, The first one called "Boot" contains and ObjectPoller class that instantiates all GameObject in the game and disable them. I made it a singleton with "don't destroyOnLoad" attribute. In the the second scene called GamePlay, There is a LevelManager that get acces to the ObjectPooler of the first scene by a function called GetPooledObject. When I boot the game by the first scene (Boot) in the Unity editor, everything works fine. But when I boot the game by the second scene, Unity editor freezes completely. I can't even shut it down. I'm do ctrl + Alt+delete to stop Unity. Is there any logic to prevent this behaviour?

 // in the ObjectPooler file

private PoolInfo GetPoolByType(PoolObjectType type)
{
    for (int i = 0; i < allPoolObjectList.Count; i++)
    {
        if (type == allPoolObjectList[i].type)
            return allPoolObjectList[i];
    }
    return null;
}

// In the LevelManager 
private void Spawn(BlocType _type)
{
    if (ObjectPooler.Instance != null)
    {
        Bloc b = ObjectPooler.Instance.GetPooledBloc(_type);
        if (b)
        {
            // logic
        }    
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Without seeing your object pooler script and how you try to access it, we can't help you. Freezing of unity can have a lot of reasons, one could be an infinite loop. If you make a dedicated boot scene, how do you expect your game to behave when skipping it? \$\endgroup\$ Commented Aug 15, 2021 at 10:30
  • \$\begingroup\$ Excuse me, I will add the script \$\endgroup\$ Commented Aug 18, 2021 at 5:55

1 Answer 1

3
\$\begingroup\$

While I can't be certain, you have most likely a loop error in your //logic bloc. But why does it work when you load the boot scene first? You have a second mistake. if (b) is always true as long as b is extended from MonoBehaviour.

public class Test : MonoBehaviour {

    private class NullTestClass : MonoBehaviour {
        
    }

    private NullTestClass demo = null;
    
    void Start()
    {
        if (demo)
            Debug.Log("It is null");
        else
            Debug.Log("This gets printed");
    }
}

This piece of code prints This gets printed even though demo has no value. MonoBehaviour has the line This class doesn't support the null-conditional operator (?.) and the null-coalescing operator (??).

Use the longer null comparison instead.

if (demo == null)
   Debug.Log("This gets printed now");
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.