(not an issue I’m having, just a super weird behaviour I don’t understand)
Can anyone explain this to me? I’ve had it happen once before on an old project, and I have no clue how it’s happening.
void Start() {
if (myScriptableObject != null) {
// whatever code here.
}
}
I am setting “myScriptableObject” inside another class, right after instantiating the object;
GameObject exampleObject = GameObject.Instantiate(stuff);
exampleObject.GetComponent<SomeClass>().myScriptableObject = someScriptableObject;
What happens if I don’t have the if statement, is it throws a null reference exception, stating “myScriptableObject” is null. But when I do have the if statement, it acts as if it’s not null, and just runs the code inside the if statement.
The only logical thing I can come up with, is that the existence and execution of the if statement gives the game just enough time to assign the value to “myScriptableObject”. This being the only thing I can come up with, makes me worried about whether this could break my game on differently performing computers, causing the code inside the if statement to simply be skipped.
I.e. I’m creating an object through code, then assigning a variable value to it right after. That same value is used in the object’s “Start()” method, inside its own class. If I check for the value being null, the code inside the if statement runs (indicating the value isn’t null), but if the null check isn’t there, I get a Null Reference Exception error.