Change scene on collision C#
Hi, I have looked all over for something to answer my question but couldn't find anything. I want to have a sphere run into a cube and change scenes. At the moment my code is not doing anything but there are no errors either.
My code currently -
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
SceneManager.LoadScene (2);
}
I am a beginner so if you could put at least a small explanation about what you suggest that would be great. Thanks!
the thing you want to collide with you have to put the script on and make sure it has a box collider and on the box collider make sure is trigger is checked and go to file-buildsettings and add all of your scenes then look at the scene you want to teleport to and look at the number and where it says Scene$$anonymous$$anager.LoadScene (2); change the number 2 to what number the scene was and that should be it.
Answer by Jawchewa · Apr 28, 2017 at 02:54 AM
A couple things that I would recommend looking into:
First, make sure that one of your gameobjects actually has a collider with the "Is Trigger" boxed checked. OnTriggerEnter won't fire if neither collider is a trigger.
Second, to use OnTriggerEnter, one of your two gameobjects must have a rigidbody attached. Depending on how your movement is working, you might also want to make the rigidbody kinematic.
See here for more details: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html
Third, one thing to consider is to use OnCollisionEnter instead of OnTriggerEnter. This way you wouldn't need Triggers or rigidbodies at all, but that may or may not be the best idea depending on your situation.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter.html
Fourth, this is probably obvious, but make sure that the gameobject that doesn't have the script has a tag set to "Player".
And Finally, you'll also want to make sure that your scene is actually added to the build settings in unity.
https://docs.unity3d.com/Manual/BuildSettings.html
Hope at least one of these helps!
Answer by kylerpc06 · Mar 01, 2019 at 04:58 PM
When i named my level even tho it was in the build settings it said it wasn't
Try using a string like this { public string newGameScene } void LoadScene () { SceneManager.LoadScene("newGameScene"); }
make sure in the inspector you set the string (newGameScene) to the name with the exact spelling as the scene you are trying to load to. this is one of the easiest ways to do it, although there is a way to get faster load times by using LoadSceneAsync Like this SceneManager.LoadSceneAsync("enter string here");
What this does is it loads the scene in the background sow the button or collider or trigger or whatever you have to trigger the loading, it just activates the scene into the foreground.
hope this helps :)
Your answer
Follow this Question
Related Questions
Won't go to next scene 1 Answer
How would I go about making a spider create a web? 0 Answers
How do I get objects to detect collisions? 2 Answers
Rotated object raycasting in wrong directions!!? 3 Answers
Objects need to collide without imparting momentum 0 Answers