if (gameobject != null) giving UnassignedReferenceException
So I'm creating a game in which the player has several playing pieces to choose from. I've created a game manager which is a singleton (and I think that might be part of my problem). The pieces are Unity UI buttons, and when they are clicked a script on the Manger is called which stores which piece was clicked on. If another piece was already selected, I want to forget about the piece and as a visual cue to the player I'm adjusting the scale. So here's my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Manager : MonoBehaviour {
public static Manager manager;
GameObject chosenPiece = null;
void Awake()
{
if (manager == null)
{
//DontDestroyOnLoad(gameObject);
manager = this;
} else if (this != manager)
{
Destroy(gameObject);
}
}
public void PieceClicked(GameObject thisPiece)
{
Vector3 scaleFactor = new Vector3(0.1F, 0, 0.1F);
if (chosenPiece != null)
{
chosenPiece.transform.localScale -= scaleFactor;
}
thisPiece = chosenPiece;
chosenPiece.transform.localScale += scaleFactor;
}
Trouble is when I run it, I get this error
UnassignedReferenceException: The variable chosenRune of Manager has not been assigned. You probably need to assign the chosenRune variable of the Manager script in the inspector.
I also tried changing around my if statement so that it was if (chosePiece == null) and even tried if(chosenPiece == null || chosenPiece.Equals(null)) but I got the same errors that way.
Most people who have similar error seem to have an issue with the script being attached to multiple objects. I've gone through and double checked, and there is only one version of the Manager Script in the scene once I've hit play (before as well). So why can't I check if my chosenPiece is null?
Are you sure it's not line 31 that's throwing the exception? You access chosenPiece there without checking if it's null (and presumably it can be null, since you're checking for that just beforehand).
It's far from clear what that localScale-related code's supposed to do, by the way. It just subtracts something then adds it back on again, so it has no effect.
So yeah, it was line 31, I misspoke before, in that because I had line 30 backwards line 31 was throwing the exception.
Just so you know, the point of localScale is to show the user what the currently selected piece is. The idea is that if no piece is selected we assign the one the user clicked on to chosenPiece and then make the UI button slightly bigger so there is a visual cue as to the Player to re$$anonymous$$d them which piece they chose.
But if a piece was already chosen before, I want it to go back to the normal size so that the pieces don't continue to grow and grow and grow each time a piece is selected. That why I both add and subtract from local scale.
Answer by szimmermann · Sep 19, 2016 at 04:01 PM
I think the error lies in line 30
thisPiece = chosenPiece;
You never assign a value to chosenPiece. In the next line you write
chosenPiece.transform.localScale += scaleFactor;
which gives you an UnassignedReferenceException because chosenPiece still has not been assigned. If you change line 30 to
chosenPiece=thisPiece;
you should get rid of that exception.
Okay thank you. You were right, I did have the line backwards. I just assumed the code was never getting that far so I didn't even check because for some reason I thought the error was on line 25. Which probably means I had another issue I fixed, and then didn't realize the error 'moved'.
Thanks
Answer by Bunny83 · Sep 19, 2016 at 04:06 PM
Uhm, the error is most likely thrown on line 31, isn't it?
We really have too much null-reference-exception questions here on UA. If you post a question about such an error post the whole complete error. You can copy and paste it from the console.
btw: this line makes no sense:
thisPiece = chosenPiece;
"thisPiece" is a parameter of your method. Why would you set it to the chosenPiece? Don't you want to do it the other way round?
chosenPiece = thisPiece;
I misspoke, line 31 was throwing the exception because of the error on line 30. So Line 30 had the error in the code that was causing the issues, just Unity was catching it on line 31. You're were completely right about doing it the other way around.
I do have a question for you, just so in future I can post better questions. You said I should post the full error message. I recreated the original problem just so I could see what the original message looked like and the most useful and relevant line was
$$anonymous$$anager.RuneClicked (UnityEngine.GameObject thisRune) (at Assets/myScripts/$$anonymous$$anager.cs:79)
Except that's because I have a lot more lines of code in my actual manager script than what I posted because I was pretty sure that none of the other lines of code had to do with that error. Also you'll notice in the code I posted I renamed the variables and functions to something that I thought made more sense in a generic sense rather than trying to explain what a rune was for the purpose of my games.
Like I said, I'm just wondering how to better post questions in the future. I notice most people use rather generic variable names when they post questions, and I don't know if that reflects what's in their actual code or not. Should I have bothered to rename my variables to something more generic?