Assertion shows up when calling GameObject.FindWithTag

Hi,

I came across an error that shows up in Editor mode.
If the game is started and stopped (by pressing the play button) an object throws the assertion !go.IsActive || !go.GetTag() != 0 in the “OnDisable” function in the following function:

GameObject.FindWithTag("Manager")

The object that is tagged as “Manager” has not been destroyed at that moment (“OnDisable” hasn’t been called for that object yet).

Any ideas how to fix this?

you must not call

GameObject.FindWithTag("Manager")

in OnDisable.
when stopping the game each gameobject receives OnDisable AND OnDestroy one after another. since there’s no order in which gameobject goes after the other, the searched gameobject could already be destroyed. maybe there’s some failovers too, but you should grab the reference earlier in the script and only check for null in OnDestroy.

In the case of the following 2 conditions, the execution of gameObject.SetActive (false) triggers the error: “Assertion failed on expression:'go.IsActive () & go.GetTag ()! = 0”.

Conditions:

  1. gameObject’s tag ! = Untagged

  2. GameObject. Find or GameObject. FindWithTag or Gamera.main is called in the OnDisable function of a component on the game object.