- Home /
Whats causing the problem in this script?
I used this code: var Status : float = 0.0; var EscapeBox : Transform;
function Update () {
if(Input.GetButtonDown("Esc")){
if(Status == 0.0){
var SpawnPos = transform.position + transform.forward * 0.9;
Instantiate(EscapeBox, SpawnPos, transform.rotation);
Status += 1.0;
}
if(Status == 1.0){
Destroy(gameObject.Find("EscBox(Clone)"));
Status += -1.0;
}
}
}
I dont get any error report, but the Escape Box doesnt appear. I guess it gets destroyed as soon as it is created. I also tried with enabling/disabling the light component, but it didnt work, too.
Answer by Julien-Lynge · Apr 26, 2012 at 05:24 PM
You're running one if statement after the other, so when status gets set to 1.0 the second if statement evaluates true and immediately destroys the object. You need to do something like if/else if
if(Input.GetButtonDown("Esc"))
{
if(Status == 0.0){
var SpawnPos = transform.position + transform.forward * 0.9;
Instantiate(EscapeBox, SpawnPos, transform.rotation);
Status += 1.0;
}
else if(Status == 1.0){
Destroy(gameObject.Find("EscBox(Clone)"));
Status += -1.0;
}
}
One more note - the way you're using the Status variable, it would probably make more sense as a bool:
var Active : bool = false;
var EscapeBox : Transform;
function Update () {
if(Input.GetButtonDown("Esc")){
if(!Active){
var SpawnPos = transform.position + transform.forward * 0.9;
Instantiate(EscapeBox, SpawnPos, transform.rotation);
Active = true;
}
if(Active){
Destroy(gameObject.Find("EscBox(Clone)"));
Active = false;
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need help syntax error 1 Answer
Destroy dead enemy object after time? script included 2 Answers
Ammo with ammobar problem. 0 Answers
Why this script is not working? 1 Answer