- Home /
Making a gameobject inactive and locating a clone. What's wrong with the script?
So I'm kind of a newbie with programming and C#, but for a schoolwork I'm trying to program a shooter game. I have a problem with my code. I'm trying to make the player inactive if it crushes to an enemy, and locate a clone on the prefab's original spot. After three times it should be game over. However, I've already used many hours to figure out how to do that and I'm getting very frustrated.
public Transform player;
public int lives = 3;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Enemy")
{
lives--;
if (lives > 0);
{
gameObject.SetActive(false);
Instantiate(player, new Vector3(3f, 6f, -17f), Quaternion.identity));
}
else
{
gameObject.Destroy(player);
Application.LoadLevel("GameOverScene");
}
}
}
This script gives me an 'Unexpected symbol' and the else here is underlined with red. Can't you use else inside of another if or what is the problem?
Answer by Scribe · Aug 24, 2015 at 11:37 AM
The error is because you have a semicolon ';' after the second if (line 10) so you never enter your if, and so you never have anything to match the else to.
The OnTriggerEnter is only called however, if this object the script is attached to (gameObject) collides with enemy, presumably therefore, you are instantiating the same object again, but if that is the case, why not just reset this ones position instead?
I did the changes and edited it to reset the position. Now it works like a charm, thank you! :)
@$$anonymous$$empsis, you should mark @Scribe's answer as correct.
Your answer
Follow this Question
Related Questions
How to check if the difference between any pair of numbers in an array is less than some value? 0 Answers
If statement seeming to give false positive 2 Answers
How to Instantiate prefab when dragging an object through a specific area? 1 Answer
Teleporting to outside of constraits 0 Answers
Unity freezing on Waitforsecond 1 Answer