- Home /
NullReferenceException: Object reference not set to an instance of an object
I'm trying to set a Parent GameObject Active within the script and the script is attached to a different empty GameObject. When I press the GUI Button, I get this error:
NullReferenceException: Object reference not set to an instance of an object
NightGUITextureButton - Copy.OnGUI () (at Assets/_Scripts_/NightGUITextureButton - Copy.js:28)
Here's the script: #pragma strict
var nightTexture : GUITexture;
var tie : GameObject;
var titletext : GameObject;
var nightText : GameObject;
var dayText : GameObject;
var tieSelection : GameObject;
var originalWidth = 1280.0; // define here the original resolution
var originalHeight = 500.0; // you used to create the GUI contents
private var scale: Vector3;
var mySkin:GUISkin;
function OnGUI()
{
scale.x = Screen.width/originalWidth; // calculate hor scale
scale.y = Screen.height/originalHeight; // calculate vert scale
scale.z = 1;
var svMat = GUI.matrix; // save current matrix
// substitute matrix - only scale is altered from standard
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
GUI.skin = mySkin;
// draw your GUI controls here:
if ( GUI.Button( Rect( 295, 350, 100, 75 ), "Night" ) )
{
tie = GameObject.Find("Ties");
tie.active = true;
titletext = GameObject.Find("chooselevel");
titletext.active = false;
nightText = GameObject.Find("Nighttext");
nightText.active = false;
dayText = GameObject.Find("Daytext");
dayText.active = false;
tieSelection = GameObject.Find("choosetie");
tieSelection.active = true;
var nightLevelInstance = GetComponent(NightLevel);
nightLevelInstance.enabled = true;
nightTexture.enabled = false;
}
//...
// restore matrix before returning
GUI.matrix = svMat; // restore matrix
}
I'm still researching a little bit more and noticed that this might be a problem due to the fact that I am trying to find an Inactive GameObject. I'll be messing around with what I have found a bit more.
You nailed it. GameObject.Find() will not find an in-active game object. One possible solution is to hide rather than deactivate the game object.
Answer by chrismcrae5712 · Feb 24, 2014 at 02:47 AM
Are both the GameObjects added to your game? Just make sure they are placed in the world because I had an error and my code was 100% but my GameObject wasn't placed in the world and I got the same error.
Answer by Linus · Feb 24, 2014 at 02:34 AM
Yes, you cant find an inactive game object. you need to store a reference before deactivating it, and use that to activate again.
Several ways to do this. Here is an example sing a Dictionary to store the reference.
static var learnedLabels = new Dictionary.<String,UILabel>();
function RegisterAllLablesInScene(){
var labels : UILabel[] = GameObject.FindObjectsOfType(UILabel) as UILabel[];
for (var label : UILabel in labels) {
core.learnedLabels[label.transform.parent.transform.name+"/"+ label.transform.name] = label;
}
}
function SetLabel(labelName : String, labelValue : String){
var label : UILabel;
// Debug.Log("Updating label name "+labelName);
if( !core.learnedLabels.ContainsKey(labelName) ){
// Debug.Log("Label was NOT known");
label = GameObject.Find(labelName).GetComponent(UILabel);
core.learnedLabels[labelName] = label;
} else {
label = core.learnedLabels[labelName];
}
if(label.gameObject.activeInHierarchy){
label.text = labelValue;
} else {
label.gameObject.SetActive(true);
label.text = labelValue;
label.gameObject.SetActive(false);
}
}
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
NullRef on my parent script 1 Answer
Is it possible to determine if OnDisable() is being called from parent.SetActive( false ) or self? 2 Answers
Is it possible to get a cloned gameObject's parent name? 1 Answer
Change SetActive(true/false) from a GameObject which is child of a different ImageTarget 1 Answer