- Home /
 
               Question by 
               Koberinha · Aug 02, 2017 at 08:08 PM · 
                c#unity 5instantiatedestroyscripting beginner  
              
 
              Is there anyway to delete clones that are local variables?
Is it possible to do this?
 void Thrust()
 {
     if (Input.GetKeyDown("w"))
     {
         GameObject Temporary_Emitter_Handler;
         Temporary_Emitter_Handler = Instantiate(Thruster, Thruster_Emitter.transform.position, 
         Thruster_Emitter.transform.rotation) as GameObject;
         Temporary_Emitter_Handler.transform.parent = transform;
         GameObject Temporary_Emitter_Handler2;
         Temporary_Emitter_Handler2 = Instantiate(Thruster, Thruster_Emitter2.transform.position, 
         Thruster_Emitter2.transform.rotation) as GameObject;
         Temporary_Emitter_Handler2.transform.parent = transform;
         GameObject Temporary_Emitter_Handler3;
         Temporary_Emitter_Handler3 = Instantiate(Thruster, Thruster_Emitter2.transform.position, 
         Thruster_Emitter2.transform.rotation) as GameObject;
         Temporary_Emitter_Handler3.transform.parent = transform;
         Temporary_Emitter_Handler.transform.Rotate(Vector3.left * 195);
         Temporary_Emitter_Handler2.transform.Rotate(Vector3.left * 195);
         Temporary_Emitter_Handler3.transform.Rotate(Vector3.left * 195);
     }
     else (Input.GetKeyUp("w"))
     { 
             Destroy(Temporary_Emitter_Handler);
             Destroy(Temporary_Emitter_Handler2);
             Destroy(Temporary_Emitter_Handler3);
     }
 }
}
I want the clones the script created to be destroyed after i release the "w" key. The Script itself works but after "else" it says i can't refer to the clones because the clone are a local variable. So is there any way to do this? (I didn't know what to put as a title, sorry if it's misleading)
               Comment
              
 
               
              Answer by Artaani · Aug 02, 2017 at 11:02 PM
It happens because you defined variable in "if" block and it can't be available from the "else" block. Just define it somewhere above. Example:
 void Thrust()
  {
 
      GameObject Temporary_Emitter_Handler = null;
 
      if (Input.GetKeyDown("w"))
      {
          Temporary_Emitter_Handler = Instantiate(Thruster, Thruster_Emitter.transform.position, 
          Thruster_Emitter.transform.rotation) as GameObject;
      }
      else (Input.GetKeyUp("w"))
      { 
              Destroy(Temporary_Emitter_Handler);
      }
  }
Actually, you want to define them as member variables of that behavior class.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                