- Home /
Script for changing levels
I need a script for changing levels once I reach a simple object (in this case a cube), can somebody give me this script?
Answer by Ashkan_gc · Jun 21, 2010 at 06:42 AM
add a collider to that cube object that you want to change level when colliding with it. check the is trigger of the collider that you attached and write something like this in a script.
function OnTriggerEnter (other : Collider)
{
if (other.tag = "player")
{
Application.LoadLevel ("levelname");
}
put the level name in the "" you can expose this as a string and set it inside the inspector or just create a manager.
It didn't work for me, do I have to use C# or Javascript?
Answer by karl_ · Jun 21, 2010 at 03:31 AM
Typically people won't just write scripts for you, but here is the documentation for what you're trying to do. Place this after an OnTriggerEnter or OnColliderEnter for your cube.
http://unity3d.com/support/documentation/ScriptReference/Application.LoadLevel.html
Also, here's the docs for Trigger enters and Collision enter:
http://unity3d.com/support/documentation/ScriptReference/Collider.OnTriggerEnter.html
http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html
Edit: Alternatively, Fishman92 has answered this question exactly here:
http://answers.unity3d.com/questions/8196/a-game-with-multiple-levels
as well as one I answered:
http://answers.unity3d.com/questions/9097/move-to-next-level-on-collide
Answer by HJJ · Oct 23, 2011 at 09:32 PM
Here's a script I got from unity-It works really well on almost and object that has a collider:
using UnityEngine;
using System.Collections;
//extend MonoBehaviour so that we can attach this script to an object
public class creditsload: MonoBehaviour
{
//A public bool so that we can change in the editor whether or not the button will quit the application
public bool isQuit = false;
//For the next three functions, the collider is absolutely necessary
//these functions will not fire off if there is no collider
//Fires off when the mouse hovers over the collider
//When the mouse is over the item, change the colour of it to
//red so that the player knows that it is interacting with it
void OnMouseEnter()
{
renderer.material.color = Color.grey;
}
//Fires off when the mouse leaves the object
//We want to change the colour of the object back to it's original when the mouse
//is no longer over it so that is exactly what we do here
void OnMouseExit()
{
renderer.material.color = Color.white;
}
//Fires off when the mouse is clicked while hovering over the object
//Here we check if the bool was set to true or not and we load the level id not
//or quit the application if true
void OnMouseDown()
{
if(isQuit)
{
Application.Quit();
}
else
{
Application.LoadLevel(7);
}
}
}
Now all you have to do is change the "MonoBehaviour" to the name you are calling the script. And if you already haven't done so, go to build settings and press add current. Then go down to the bottom where it says (7) and change it to the level number shown on you build settings. This should work.