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);
}