Weird error appearing after playing in editor?
Hey guys, got a bit of a weird error, hopefully someone here can help.
So I have created a custom class (does not derive from MonoBehavior).
Then I have a variable for the class, in the Start() method I set it to 'null', first time I play it in editor, everything is fine, (pickups/weapon switching etc..), but then when I play it again after a while, the variable is not null, even though I set it to null in Start().
Here's the code to make it
[System.Serializable]
public class Weapon
{
public string weaponName;
}
public class PlayerFPS : MonoBehaviour
{
public Weapon secondary;
void Start(){
secondary = null;
print(secondary == null); //this returns True the first time I play, but False after
}
}
It seems that for some reason after playing in the editor, the variable stops setting to 'null'.
I do a lot of Instantiating and Destroying, so perhaps, the issue is somehow related to Garbage Collector or Cache?
Thanks in advance!
If you want the weapon name to be null, write secondary.weaponName = null, else just create a try catch method. In try, write print(secondary == null), and in catch, write secondary != null, then say secondary = null.