- Home /
Switching Scene Immediately
Hello! I have a script I am using to Switch the scene once my player is hit by the enemy. Instead, the scene switches when I hit play.
Here is my script:
public class DestroyPlayer : MonoBehaviour {
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "AI Bullet")
Destroy(gameObject);
SceneManager.LoadScene ("Too Bad");
}
}
Answer by bobisgod234 · Jul 24, 2017 at 01:12 AM
If you don't use the curly braces ( the {'s and }'s) to form a code block, then the if statement will only work on the next immediate statement (Destroy(gameObject)).
This means that you are loading a scene whenever your player enters any trigger at all, not just a bullet. Place the statements you want to execute based on the if condition in curly braces, like:
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "AI Bullet")
{
Destroy(gameObject);
SceneManager.LoadScene ("Too Bad");
}
}
Answer by shadowpuppet · Jul 24, 2017 at 06:23 PM
what bobisgod said. it is the equivalent of writing: if (other.gameObject.tag == "AI Bullet") Destroy(gameObject); if (other.gameObject.tag == "AI Bullet") SceneManager.LoadScene ("Too Bad");
only more efficient
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to switch between scenes? 0 Answers
Does anyone know how to look into this sort of thing? (Desciption) 3 Answers
Create gameobject after scene change 1 Answer