Hello! im in need of a script that simply moves you to a random scene by touching an object
What have you tried so far? Post the code that is giving you difficulty, and please use code tags.
by touching an object I am assuming that you are using box trigger or some kind of a collider.
Just use the below script that I have added on Start() function.
using UnityEngine.SceneManagement;
//Assuming you have 4 levels or scenes
void Start()
{
int index = Random.Range(1,4);
SceneManager.LoadLevel(index);
}
No, not quite. This code:
is a little bit of a lie. Go see the docs for int Random.Range(int, int)
and you’ll see the above code will only generate 1, 2 or 3.
Try it yourself if you doubt me:
void Update()
{
int i = Random.Range(1, 4);
Debug.Log(i);
}
Let it run as long as you like. It will never generate 4.
This highlights the perils of jumping to multi-year-old threads. Generally, please don’t necro-post.
If you have a new question, make a new post. It’s FREE!!
1 Like