Inspector Variables Not Updating/Changing?

Been following a simple point and click tut, done everything just as he does, minus a comment here and there. My problem, is that when I try to change the variables in the inspector (gameTime and such) they don’t update/change when I click play. He changes the same variables and clicks play and they DO update. I’m at a loss, I’m completely new to javascript / coding in general, he is using 3.5 and I’m using the latest. I’ve tried messing with the code, but to no satisfactory results. If I could just get this problem and one or two other minor ones ironed out, I could call this “game” complete and move on to the next tut. Any help would be appreciated. As I understand it, they’re simply acting like they’re hardcoded. (Note: Before pasting the code I messed with it a tiny bit, changed values on var gameTime to 0 from 5 and other little things, it was all just from a higher number to 0 though, thought that maybe they had to be on 0 to be able to be changed in the inspector. Other than the actual values for the vars, it’s all as he has it. And it wasn’t working when the values were exactly the same.)

//Inspector Variables
var tagName             :String;          //allow designer to setup tag in inspector
var rayDistance         :float = 0;       //length of the ray for our raycast
var score               :int   = 0;       //score for our player
var gameTime            :float = 0;      //the amount of time the game will last
var loadWaitTime        :float = 0;     //amount of time to wait before we load the next scene
var numberOfPointsToWin :int   = 0;       //number of points to win game

function Start()
{
   InvokeRepeating ("CountDown", 1.0, 1.0);   //repeat the countdown every second
}
//update is called every frame
function Update() 
{
  if(Input.GetMouseButtonDown(0))
  {
    print("Pressed left click.");
    var hit : RaycastHit;
    var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    if(Physics.Raycast(ray, hit, rayDistance)) 
    {
      if(hit.transform.tag == tagName)
      {
       var enemyScript = hit.transform.GetComponent(scriptActorEnemy);
       enemyScript.numberOfClicks -= 1;       //reduce the number each click
       //check that the object is at 0 before adding the points to the score
       if (enemyScript.numberOfClicks == 0)
       {
       score += enemyScript.enemyPoint;       //add point to our overall score
       }
      }  
      else
      {
        print ("This is not an enemy!");
      }   
    }
  }
}
function CountDown()
{
   if(--gameTime==0)               //subtract from gametime
   {
      CancelInvoke("CountDown");   //cancel the countdown
      if (score >= numberOfPointsToWin)
      {
         Application.LoadLevel ("sceneScreenWin");
      }
      else
      {
         Application.LoadLevel ("sceneScreenLose");
      }
   }
}
//
function OnGUI()
{
   GUI.Label(Rect(10, 10, 100, 20), "Score: " + score);
   GUI.Label(Rect(10, 25, 100, 35), "Time: " + gameTime);
}

Try moving the variables into an Awake or Start function. I generally do not give values to variables outside of a function. By putting them into Awake, they will show up as “null” in the inspector but once you hit Play, they will pull the values from Awake. Once in Play mode though, you can change the values in the inspector to modify the game behavior.

Sometimes I find Unity just can get a little funky though and I have to right click my script in the inspector and hit “reset”. That will put all the values back to what they are in the code. If the Awake function doesn’t do what you want it to do, try reseting the script.

The Awake function is a built in function to Unity. When you hit Play, code inside it is run before anything you place into the Start function.

var tagName : String;
var rayDistance : float;
var score : int;
var gameTime : float;
var loadWaitTime : float;
var numberOfPointsToWin;

function Awake()
{
tagName = "";
rayDistance = 0;
score = 0;
gameTime = 0;
loadWaitTime = 0;
numberOfPointsToWin = 0;
}

Edit: Never mind, I got the destruct code to work. I was placing it in the script I had pasted down below, which is my enemy script. Obviously I had to create a new script and place it with the particles gameobject, wasn’t catching on to that at first. And if by any real long shot of a chance, someone needs it, heres the code for the destruct, as stolen from on here some where.

    var additionalDestructionDelay : float;
    private var thisParticleSystem : ParticleSystem;
     
    function Start () {
     
        thisParticleSystem = this.GetComponent(ParticleSystem);
       
        if (!thisParticleSystem.loop) {
            Destroy(this.gameObject, thisParticleSystem.duration + additionalDestructionDelay);
        }
     
    }

Oh man. Lol. I toyed around with what you said a bit, couldn’t quite getting it working the way I wanted. Then I managed to mess up this script and another script that it’s linked to, made everything pretty unplayable. After a long time of messing with stuff though, I found a couple mistakes (couldn’t even tell you what they are now) and fixed them, got my scripts pretty much exactly as they were before I even posted here, and the variables working, or at least so far as I tested, just as they should be.

I thank you for the help.

I have another problem. I need to get a particle to play once then destruct. In the tut, he just uses an autodestruct checkbox in the particle options, however, that was 3.5, and this isn’t and that box is no longer there, so I need to do it with code. I searched a little bit on how to script it, I think I even managed to get the code right, I’m not for sure, I think I was inserting it in the wrong place though, or maybe I didn’t have it right. Well, heres the code for it, if I need to create an new thread I will, was just hoping to save the space if it is ok.

//Inspector Variables

var shapeColor         : Color[];
var numberOfClicks     : int = 2;
var respawnWaitTime    : float = 2.0;
var explosion          : Transform;
var enemyPoint         : int =1;    //value of the enemy object

//Private Variables
private var storeClicks : int = 0;

//start is only called once in the lifetime of the behavior
function Start ()
{
 storeClicks = numberOfClicks;
 var startPosition = Vector3 (Random.Range(-6,6),Random.Range(-4,4),0);
   transform.position = startPosition;
   RandomColor ();
 
}


//Update is called every frame
function Update () 
{
 if(numberOfClicks <= 0)
 {
   if (explosion) 
   {
       Instantiate(explosion, transform.position, transform.rotation); //create an explosion
 
         
   }
   if (audio.clip)
   {
       audio.Play();
   }    
   var position = Vector3 (Random.Range(-6,6),Random.Range(-4,4),0);
   RespawnWaitTime ();
   transform.position = position;
   numberOfClicks = storeClicks;
 }
}
//RespawnWaitTime is used to hide an object for a set amount of time and then unhide it
function RespawnWaitTime ()
{
   renderer.enabled = false;
   RandomColor ();
   yield WaitForSeconds (respawnWaitTime);
   renderer.enabled = true;
}
//RandomColor is used to change out the material of a game object
function RandomColor ()
{
   if (shapeColor.length > 0)
   {
     var newColor = Random.Range(0, shapeColor.length);
     renderer.material.color = shapeColor[newColor];
   } 
}

If someone could type out the code and tell me where to insert it, I believe I can finally call this thing done and move on. Don’t want to call it done when it keeps creating an explosion particle effect everytime and doesn’t destroy/delete them, as they just keep stacking up and I’m guessing if this game was actually going to be played that would lead to extra resource usage and eventually slow down.