Switch Scene on Collision with a Box
Hi. In my project I have multiple Scenes and in each scene there are two boxes, which should load the next bzw. the previous scene if the FPSController collides with one of them.
public int sceneIndex I´ve written a Script with the functions public void OnCollisionEnter(Collision col) { if(col.GameObject.name == "Player") { LoadSceneByIndex(sceneIndex); }
}
private void LoadSceneByIndex(int index) { SceneManager.LoadScene(index); }
If i collide with the box, nothing hatppens. Does anybody of you find the error and/or could tell me a way how it will work?
I´m grateful for every helpful answer.
Answer by Quincyz · Jul 21, 2017 at 12:13 AM
#pragma strict
//YOU MUST HAVE THIS FOR IT TO WORK
import UnityEngine.SceneManagement;
//Put the scene place number here
var levelToLoad = 1;
//This is the on collide trigger, also must have "Is Trigger" on object.
function OnTriggerEnter(other:Collider)
{
//This "Player" can be changed, if the object "player" runs into the object this code is on then the code will activate
if(other.gameObject.name == "Player")
{
//This is the scene name
SceneManager.LoadScene("Level001");
}
}
Your answer
Follow this Question
Related Questions
How do I make player attack with LMB if the enemy is within range? 0 Answers
How to trigger a BoxCollider with a RigidBody? 0 Answers
Debugging Collision script - Help required to understand behaviour 2 Answers
I have two different objects I would like to check when A and B gameObjects are collided ? 1 Answer