- Home /
 
Unity drops my variables after a couple times
So, this one has me scratching my head:
I've assigned a couple of variables in a script that reference the player motor / character controller / and mouselook scripts on the player. I set the player up in the inspector, and the first couple of times the script runs, everything is fine. However, if the script is called a third time, the variables have disappeared from the inspector, and I get a null reference exception. The truncated script looks like this:
 var playerScript:CharacterMotor;                
 var playerScript2:FPSInputController;
 
               The following is not in the same function. (Actually, I know, the variable declarations are not in a function at all) This script was shorted for clarity's sake. Also, for clarity's sake, the playerscripts were disabled in a separate function. If this code snippet is not enough to diagnose the problem, please let me know. Thanks, and God bless.
 if(itemPosition == 4)
         {
             cam1.enabled = true;
             cam1.active = true;
             cam2.enabled = false;
             cam2.active = false;
     
     ShopCameraSwitch._inShop = false;
             
             Time.timeScale = 1;
             playerScript.enabled = true;
             playerScript2.enabled = true;
             playerScript = GetComponent(CharacterMotor);
             playerScript2 = GetComponent(FPSInputController);
             //playerScript2.GetComponent("MouseLook").enabled = true;
             camScript.GetComponent("MouseLook").enabled = true;
             
 }
 
              Your answer