Unity Bug? null reference exception, Only its not null...
Hey, I have a slightly confusing issue..
Unity is throwing a Null Reference Exception at me:
NullReferenceException: Object reference not set to an instance of an object
Drag_Equipment.OnMouseDown () (at Assets/Scripts/Kitchen Scene/UI Scripts/Drag_Equipment.cs:285)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
When I go to that code line and check it out (Drag_Equipment.cs:285), its' throwing the error at the GameObject reference 'currentStation' on the GameObject 'equipment1'.
So, this seems like it should be pretty easy to solve, however when I pause the game and go in to the inspector I can see that 'currentStation' definitely has its value set...
And to further confuse me, 'currentStation' is pointing at separate GameObject called 'station1', now 'station1' is also pointing back at GameObject 'equipment1' on which the variable 'currentStation' exists in the script 'Drag_Equipment.cs'. And the confusion arises when I click on the reference on 'station1' pointing to 'equipment1' (where variable 'currentStation' is), it highlights the 'equipment1' in the Hierarchy as it should, and then when I unpause the game, the error is no longer being thrown, and things work as intended.
Quick edit/update here... Acutally I dont even have to click on 'station1's reference to 'equipment1', I simply have to click on the GameObject in which 'currentStation' is referencing, in the Hierarchy and then when I unpause, no more error, and it works as it should...
Any Ideas what might be causing this, or what I could do to fix it?
I've tried a couple things including resetting the value of 'currentStation' to what it should be, yet the error persists, until I pause, click it's reference in the inspector and unpause.. Its quite confusing..
Here is the bit of code which is throwing the error:
void OnMouseDown(){
// !!! if the recipe has finished cooking, and the player is trying to plate, instantiate the finishedRecipePrefab with artwork
// and allow it to follow the cursor position based on the 'mouseDrag' script attached to it
//Debug.Log ("mouse is down on " + this.gameObject.name + " on station " + GetComponent<Drag_Equipment> ().currentStation.name);
//GetComponent<Rigidbody2D>().WakeUp(); // kick this thing so it realizes its on a station and can properly have its 'currentStation' set.
Debug.Log("!!!!!!! Current station is" + currentStation.name);
Debug.Log("!!!!!!! Finished cooking is" + finishedCooking);
Debug.Log("!!!!!!! Current station recipe is " + currentStation.GetComponent<CookingStation> ().recipe);
Debug.Log ("!!!!!!! Current staiton status is " + currentStation.GetComponent<CookingStation> ().stationStatus);
if (currentStation != null) {
if (finishedCooking && currentStation.GetComponent<CookingStation> ().recipe.recipePlated != true && currentStation.GetComponent<CookingStation> ().stationStatus == CookingStation.Station_Cooking_Status.Recipe) {
That very last if statement.. Somehow its getting past the first null check even. And also, all of the debug,log's I have above checking each part of the if statement are all able to be printed out to the console just fine, so its showing me that it has a value for everything in that if statement while still throwing the null reference exception...
Your answer
Follow this Question
Related Questions
MonoBehaviour Update called twice, fields being set to null 0 Answers
NullReferenceExeption mais variable initialisée 1 Answer
Disable array of components 1 Answer
help with a camera bug 0 Answers
[Forum bugged, can't delete] 0 Answers