- Home /
Local Variables
Hello, all. This should be simple and quick. I have a prefab. This prefab has a script that is a timer. The script has a variable that the time of the timer is stored in. The problem is, I will have more than one instance of this prefab, and eery time I instantiate a new one, it picks up where the timer from the first one left off. Is there any way I can make it so that the other duplicates do NOT use the same variables, but keep separate copies of the same variable for themselves? thanks - YA
Here is the code.
var yellowTex : GUITexture;
private var xpos : float;
private var ypos : float;
private var scale : float = -0.8;
private var scaledelta : float;
private var position = Vector3(-0.002426388,0.205147,1);
private var xdelta : float;
private var ydelta : float;
private var created : boolean;
private var yellow : GUITexture;
var TravelTime : float = 2;
var TimeStart : float = 0.0;
//Beginning coordinates are (-0.002426388,0.205147,1);
//Ending coordinates are (-0.02,0.02,1);
function Update () {
if(Input.GetButtonDown("Vertical")){
yellow = Instantiate(yellowTex, position, Quaternion.identity);
created = true;
yellow.transform.localScale = Vector3(-0.8, -0.8, 1);
}
if(yellow){
//calculating x and y positions
xdelta = ((-0.02 - -0.002426388)/TravelTime)*TimeStart;
ydelta = ((0.02 - 0.205147)/TravelTime)*TimeStart;
ypos = 0.205147 + ydelta;
xpos = -0.002426388 + xdelta;
//updating position
yellow.transform.position = Vector3(xpos,ypos,1);
//calculating scale values
scaledelta = ((0 - -0.8)/TravelTime)*TimeStart;
scale = -0.8 + scaledelta;
//updating scale
yellow.transform.localScale = Vector3(scale,scale,1);
}
if(created == true){
CountUpwards();
}
}
//The timer function
function CountUpwards(){
TimeStart += Time.deltaTime;
}
Answer by DaveA · Nov 30, 2011 at 04:21 AM
Posting code would help. My guess is your variables are 'static'? Should not be.
Answer by Eric5h5 · Nov 30, 2011 at 04:24 AM
Variables are separate instances by default. I assume you're using static variables; you should never use them unless you're sure you only want one instance.
Here is the code. It is attached the prefab. Every time I instantiate a new one, It picks up where the other one left off, not where the code tells it to. The math you see is simple tweening of GUITextures.
I'll post the code as an answer
Sorry. i changed it and put it into the original post. So how do I fix this?