0
\$\begingroup\$

I have a Main Menu with level selecter up to 5 different scenes on one Map. I also have a MenuManager which doesn't get destroyed between the scenes to gather the bool information of level finished or not. When I start the game and change for example level 1 and move back to the Main Menu again the MenuManager gets copied. I tried this at first:

public static GameObject _menuInstance;

void Awake()
{
    if (_menuInstance == null)
    {
        _menuInstance = this.gameObject;
    }
    else
        Destroy(gameObject);
}

My problem with that is that the copied GameObject that get's destroyed is the wrong one.

This is the copy that gets created after I switched the scenes and back to Main Menu.

This is the copy of the original

This is the original MenuManager that should be the _menuInstance Object and the other one should be destroyed.

This is the original and should be the _menuInstance

How can I not delete the original one and delete the one without the references?

I come up with something but I am not sure if this is the right way to do this.

    //I changed the code in the Awake() function to
    void Awake()
{
    if (_menuInstance == null)
    {
        _menuInstance = this.gameObject;
    }
}
//I checked one of the missing references to be null and then destroy the fake copy.
Update()
{ 
   if(_startTheGame == null)
     Destroy(gameObject);
}
\$\endgroup\$
1
\$\begingroup\$

Because this is what happens wit your code:

  • In the first scene, _menuInstance is null, therefore the created GameObject is assigned to it.
  • Then in the second scene, the same GameObject checks and _menuInstance is not null, so it gets destroyed.
  • Then the copied GameObject evaluates, now _menuInstance is not null, thus that GameObject is assigned to that reference.

So, you have to add an additional check on the else:

void Awake()
{
    if (_menuInstance == null)
    {
        _menuInstance = this.gameObject;
    }
    else if(_menuInstance != this)
        Destroy(gameObject);
}

So you only destroy the GameObject if _menuInstance is not null and if that same _menuInstance is not the evaluating GameObject. Hope it helps.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Well it's kinda near the solution but it is still keeping the GameObject without all the references like I've shown in the pictures. What's weird is that if I do a check of any of the missing references in the Update() function is null then Destroy(gameObject); it works however, the right GameObject isn't destroyed but the other copy. Does this affect anything? I mean everything works now. \$\endgroup\$ – Daniel Apr 9 '19 at 0:21
  • \$\begingroup\$ you could use a bool function that returns whether the class has the required references different from null. So if it returns false, should be destroyed. \$\endgroup\$ – LifGwaethrakindo Apr 9 '19 at 0:24
  • \$\begingroup\$ Yes that should work too! \$\endgroup\$ – Daniel Apr 9 '19 at 0:26
  • 2
    \$\begingroup\$ The original menu manager shouldn't have its Awake called a second time, unless there's something else funny going on here... \$\endgroup\$ – DMGregory Apr 9 '19 at 0:28
  • \$\begingroup\$ But I only can do this in the Update() function I tried it in the Start function after the Awake function because it will immediately delete the original before even passing the references. So even in the Start function it will get deleted, it works fine for me in the Update function. \$\endgroup\$ – Daniel Apr 9 '19 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.