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);
}
Did you ever solve this? I am having the same exact problem, and no amount of Googling and asking has solved it. Thanks!
Answer by allenallenallen · Jan 09, 2016 at 12:31 PM
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.
It's definitely compiling. Any change I make, however small is taking effect.
Answer by JoshuaMcKenzie · Jan 09, 2016 at 03:11 PM
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.
Good point. I'll try that out. It's definitely not re-instantiating because I can see the object moving across the screen (re-instantiating should keep making a new one at the spawn).
I did that test, and I'm pretty sure this is a failure to get the BulletCode component from the object correctly. I can see the get/set values incrementing, but there's another get that's co$$anonymous$$g from the call in the gun that's always returning zero.
Answer by richardgengle · Aug 07, 2020 at 03:14 AM
i am having similar problems... but for your case,,,, how is bc.getlifespan working... i dont see the connection there
Answer by mannysayah · Feb 20, 2021 at 10:29 PM
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.