Variables not updating

I feel like I’m clearly missing something here. I’ve got a very simple class that moves an object forward. I’m trying to keep track of its lifespan, so I’m incrementing an int every update. However, when I try to call GetLifeSpan() that value is zero. What’s even weirder is that, if I print the value in the Update function, it also appears as 1 (as if it was just updated from zero). Also, the “wait” int, which is incrementing correctly and prints correctly in Update() appears as zero in GetLifeSpan().

What am I missing here? I’m so confused.

	private int wait = 0;
	private Vector3 movement;
	public int lifespan = 0;

	// Use this for initialization
	void Start () {
		movement = transform.forward;
	}
	
	// Update is called once per frame
	void Update () {
		lifespan = lifespan + 1;
//		print (lifespan);
//		print (wait);
		if (wait == 0) {
			wait = 20;
			//print (movement);
			transform.Translate(new Vector3(0,0,0.05f));

		} else {
			wait--;
		}
	}

	public int GetLifeSpan() {
		print ("getting ls");
		print (lifespan);
		print (wait);
		return lifespan;
	}

Alternatively, am I screwing something up in the code that spawns it? The above code is in the BulletCode class, and the below code is basically the gun that fires it. Also, can anyone explain to me why bc = bullet.transform.GetComponent(BulletCode); doesn’t work (it has an error about it not being a System.type… I’ve seen other code that looks to be doing this though).

	private int test;
	public GameObject bullet;
	public BulletCode bc;

	// Use this for initialization
	void Start () {
		test++;
		MakeBullet ();
	}
	
	// Update is called once per frame
	void Update () {
		//print (bc.getLifeSpan());
		if (bc.GetLifeSpan() > 10) {
			// never gets called
			Destroy(bullet);
			MakeBullet ();
		}
	}

	public void MakeBullet () {
		Vector3 pos = transform.position;
		pos = pos + new Vector3 (0f, 0.3f, 0f);
		Instantiate (bullet, pos, transform.rotation);
		bc = bullet.transform.GetComponent("BulletCode") as BulletCode;
		//bc = bullet.transform.GetComponent(BulletCode);
	}

Script seems fine to me. Make sure you saved the code in your script editor and that Unity had the time to compile it. THEN you can try clicking play again.

sounds like you have a different source thats affecting the value (or you’re constantly instantiating a new instance per frame, but that should have been obvious to you), try turning the value into a property and print to console when the value is changed,

private int _lifespan = 0;//backing variable
public int lifespan
{
    get
    {
        return _lifespan;
    }
    //make it private set so that the variable is read-only
    private set
    {
        _lifespan = value;
        Debug.Log(value);
    }
}

with a property you can track any access to the variable. And benefit of the Debug.log is that it’ll get you a stacktrace to see if the value is getting changed elsewhere.

i am having similar problems…
but for your case, how is bc.getlifespan working… i dont see the connection there

You need to reset the Script Component in Unity to see changes.

To do that, click on the ellipsis icon (shown below) and then click Reset. The inspector will now show the new values.

176342-setttings.jpg