- Home /
How to make a variable it's original value?
I made a script that counts the number of times and object collides with it and when it reaches 0, the object is destroyed and later teleported to another location. Here is the script: var lives = 5; var spawnPoint : Transform; var seconds = 2.0;
function OnTriggerEnter(){
(lives) = (lives) - 1;
if((lives) == 0){
Invoke("LiveAgain", (seconds));
gameObject.SetActive(false);
}
}
function LiveAgain() {
gameObject.transform.position = spawnPoint.position;
gameObject.SetActive(true);
}
How could I make it so then the variable "lives" is changed back to it's original value after spawning again?(For example, if lives was originally ten, I would want it to be 10 after it spawns again at "spawnPoint")
Answer by Dave-Carlile · Jun 06, 2013 at 08:08 PM
Just set the variable to the value you want in your LiveAgain
function.
function LiveAgain()
{
lives = 10; // <-- just set the variable back to the value you want
gameObject.transform.position = spawnPoint.position;
gameObject.SetActive(true);
}
Thanks, but you didn't get what I meant. You did give me an idea, though. I edited the script and if you want to know what I was trying to do, here is the script:
var lives = 5;
var spawnPoint : Transform;
var seconds = 2.0;
private var hearts = 0;
function OnTriggerEnter(){
(lives) = (lives) - 1;
(hearts) = (hearts) + 1;
if((lives) == 0){
Invoke("LiveAgain", (seconds));
gameObject.SetActive(false);
}
}
function LiveAgain() {
(lives) = (hearts);
(hearts) = 0;
gameObject.transform.position = spawnPoint.position;
gameObject.SetActive(true);
}
How is my answer different from what you asked for in your question?
Because if I did your script, I would need to change the number of lives every time I made a game object. This way, I made a private var so then the number of lives is always the same as it was in the beginning.
That's fine, but that wasn't part of your original question, so I'm not sure why you said "I didn't get what you meant", because I answered the question you asked. In any case, it's irrelevant. Glad I was able to help :)