Hi I’m following an RPG course and I’m experimenting with adding my own features and I’ve just come across something I find very odd which isn’t making sense.
Basically the AI and the player both share the same Fighter.cs script, which was fine until I needed the fighter to check something from the AIController.cs.
Now I know in ideal circumstances I would give the AI and the player separate fighter classes so when calling:
_aiController.GetComponent<AIController>();
There won’t be any null reference because the player obviously doesn’t have an AIController attached.
Now, this leads me to my question which I’m finding very odd, It makes sense the player stops working properly, but for some reason every other enemy in the scene only if the player is active Fighter.cs also messes up as well.
So does one Gameobjects instance of a class having a null reference affect every other separate GameObjects instance own version of that class? Hopefully that made sense I found it difficult to word this question.
You’re thinking of code as some magic thing with unseen mysterious effects. This is not how code works. Code executes line by line, in order, and in the case of Unity scripting, only one line of code anywhere is running at a time.
Your question is basically extremely vague and not really enough detail to help you. Code doesn’t do anything magical. If you have an exception, you really need to fix it, because exceptions cause your code to stop dead, leaving unfinished work behind and leading to undefined and undesired behavior.
I’m not thinking of code as something magical I’m trying to logically figure out what is causing it to happen…
My question was basically does one Gameobject that has an instance of a class that has a null reference potentially or always cause other game objects with their own separate instance of a class to also stop working correctly.
So from my interpretation of what you’re saying is that, yes a single GameObject with a separate version of an instance can cause others and even completely class instances to stop working because that individual line can cause the code to stop completely?
Right now we’re speaking in vagaries that are abstract and difficult to understand. If you share your actual code and the actual issue you’re running into, you may be able to get better help.
You need to mentally separate the concept of a GameObject itself (the thing that appears in the hierarchy window of Unity and exists in the game) from a reference to a a GameObject (aka. a GameObject variable). A variable is a reference to an actual object. If the reference isn’t pointing at an actual object it is a null reference. Trying to use a null reference causes a NullReferenceException (as you have seen).
Each reference variable is completely separate from other reference variables. There is no magical link between them that would cause one null reference to “break” other references.
An exception will cause your code to stop running. In the case of Unity, this means that that particular Unity callback method will stop running and Unity will move onto the next. So for example if the exception happens in Update, the rest of that Update call will not run, and Unity will move on to calling Update on the next script in its list.
To be more precise, having null is nothing bad itself, you can compare null variable to other objects or null, but you can’t call it’s methods or properties as it’s not allowed.
Also the exception will “bubble up” until somehting catch it, so to answer your question in simple words, yes if you get error at some point it might, and probably will cause problem in rest of your code (or at least in portion of it).
If you want to be sure you are not using null just check if it’s null:
if (something != null)
{
something.DoYourStuff();
}
Ever since adding this like I say I understand it’s messing up the player because the player doesn’t have an AIController.
But this is also causing the enemies that do have an AIController to also have a null reference with that awake call but also an additional null reference in the AIController trying to reference the Fighter.cs with:
I have found through more testing even with the player completely deleted the enemies are still getting a null reference calling:
_aiController.GetComponent<AIController>();
When an AIController is attached.
So now I’m completely baffled, I would have written a completely different question if realizing this, although the only thing I can think of. Is now the Player being deleted while testing this because the player is required in other parts of the code if this is causing things to break.
If my original question still isn’t clear at this point unfortunately it’s a failure on my side of being unable to clearly explain it. Ill just continue and hopefully discover the answer in the future.
My pleasure! Generally anytime you start imagining undocumented connections between separate things in a computer, it’s almost certainly not the answer. Either there IS a connection, or there is not, and it will almost always be documented. Back in the day with older computer languages (C, C++, assembly, etc) it was quite a common thing for System A to have a bug that incorrectly modified state in System B, but those wild west days are (largely) in the past.