- Home /
              This post has been wikified, any user with enough reputation can edit it. 
            
 
             
               Question by 
               Lazy08 · Oct 14, 2013 at 09:13 PM · 
                c#gameobjectinstantiatedestroy  
              
 
              References to GameObject become null
I have a C# script that is supposed to handle Quick Time Events. I have it so that the event is activated via ActivateQTE(gameObject), so that I'll know who called it. However, once the quicktime event is done, the variables I stored on activation are all Null! Specificaly, button1, button2 and called become null. Am I doing something wrong?
 bool isActive = false, canQTE = true;
 public float duration;
 float timer;
 int count;
 public GameObject key1, key2;
 GameObject button1, button2, called;
 
 public void ActivateQTE (GameObject go)
     {
         isActive = true;
         timer = 0f;
         count = 0;
         called = go;
     }
     
 void Update ()
 {
     if (isActive) {
         timer += Time.deltaTime;
         //This bit runs only once when it activates.
         if (canQTE) {
             canQTE = false;
             //Creates the buttons onscreen
             button1 = (GameObject)Instantiate (key1);
             button2 = (GameObject)Instantiate (key2);
         }
         if (timer < duration) {
             //Do QTE
         } else {
             //Here is where the issue starts. "button1", "button2" and null, so is "called"
             Destroy (button1);
             Destroy (button2);
             if (count >= clearAmmount) {
                 called.SendMessage ("QTE_Clear");
             } else {
                 called.SendMessage ("QTE_Fail");
             }
             isActive = false;
             canQTE = true;
         }
     }
 }
Thanks in advance
               Comment
              
 
               
              $$anonymous$$y bad! I'll edit it in, but its button1, button2 and called
Could you post the console output to help narrow it down.
Answer by chippy-cheese · Oct 14, 2013 at 10:11 PM
when you Instantiate an object make sure you do it "as GameObject". It might classified as something else like a transform. EX: button1 = (GameObject)Instantiate (key1) as GameObject;
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                