- Home /
how to switch between scene
hi everyone, im trying to switch from one level to the other here is my code
var myLevel : String;
function OnCollisionEnter (myCollision : Collision) { if(myCollision.gameObject.name == "Floor"){ Application.LoadLevel(myLevel); } }
but it just work when i add a rigibody to my player but when i did it my player start jumping like crazy
Answer by BarkShark · Jun 20, 2011 at 11:53 AM
In unity go to File >> Build settings. A new window will pop up. Just hit add current to add you're current scene. You also can drag your scenes from your project folder. Then they're appear numbers next to the name of your scene. In your script use this instead:
function OnCollisionEnter (myCollision : Collision)
{
if(myCollision.gameObject.name == "Floor")
{
Application.LoadLevel(// The number next to the scene in the build settings window);
}
}
And if you change the collider to a trigger? In your script you just have to use OnTriggerEnter ( myCollision : Collider)ins$$anonymous$$d of
function OnCollisionEnter (myCollision : Collision)
Answer by EJ6612 · Jan 05, 2017 at 08:18 AM
It has now been updated to this:
using UnityEngine.SceneManagement;
// In a function
SceneManager.LoadScene(1); //replace the int with level number
Answer by edovman · May 30, 2015 at 07:07 PM
yes you should make it a trigger so the player doesn't bounce off
I have spent countless weeks making countless scenes on a game I have been developing, and I can't get any damn javascript script to switch me between scenes by making contact with an objects collider. I was really hoping that you guys could help, but none of your scripts work, it keeps saying that it was expecting something different, and if I switch it to what it was expecting it just screws the script even more. Please, any 'updated' assistance would be greatly appreciated.
Damn me, I shouldn't ask such $$anonymous$$iscule questions. I take it back, I actually found a tutorial on how to do so. Thanks anyways.
Answer by Vollmondum · Jul 09, 2017 at 10:56 AM
It's currently
SceneManagement.SceneManager.LoadScene("sceneName");
$$anonymous$$oreover no rigidbody is required if one of collider is set to "Is Trigger". The other one shouldn't be one. Another issue happened the other day, it couldn't detect collision between two simple shape colliders. You want this:
function OnTriggerEnter(myCollider:Collider)
{
if(myCollider.name == "whatever")
{
Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene("sceneName")
}
}
Or ins$$anonymous$$d of "sceneName" you can type the number of your scene in build settings
I tried that, but Unity gave me "Assets/Scripts/ExitCredits.cs(15,13): error CS0103: The name 'SceneManagement' does not exist in the current context" Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExitCredits : MonoBehaviour
{
public bool shouldExit = false;
// Update is called once per frame
void Update()
{
shouldExit = Input.GetKeyDown(KeyCode.Escape);
if (shouldExit)
{
SceneManagement.SceneManager.LoadScene("Entrance");
}
}
}