Change scene
Hi there,
I made a 2d cube and i attached a script that if you click the cube, the Player game object walks towards the cube. That works.
Now I want to change the scene as soon as the player hits the cube. If found this script which should work.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class SwitchLevel : MonoBehaviour {
public string levelToLoad;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
SceneManager.LoadScene(levelToLoad);
}
}
}
But when the Player hits the cube, no new scene is loaded.
I have attached the Player-tag to the player game object (which walks towards the cube), i placed the name of the scene in the empty box in the inspector and attached a box collider to the cube and the player en made the cube a trigger.
I hope someone can help me out with this problem. Thank you in advance!
Cheers,
Jonah
Answer by mrwmrutski · Aug 02, 2016 at 09:48 PM
void OnCollisionEnter(Collision myCollision)
{
if(myCollision.gameObject.tag == "Player")
{
Application.LoadLevel("Scene Name");
}
}
I found this laying around and is not mine: Answer by Vice_Versa · Sep 14, 2015 at 01:13 AMhttp://answers.unity3d.com/questions/1066454/on-collision-enter-next-scene.html link text
I myself am a Very newbie but this example is naming the Collision to the game Object being the active player. Your example is vague left opened, if you get my point, insert it and SEE?? It is my guest and only offered as I am learning as I go. Hope it helps you out ...Peace..... PS reply back if it does,thanks
[1]: http://answers.unity3d.com/questions/1066454/on-collision-enter-next-scene.html
@mrwmrutski Thank you for answering.
Newbie myself also ;)
Too bad, this didn't work. What I read in tutorials is that the code you posted doesn't work in Unity 5.3. When you attach it, Unity tells you that you have to use Scene$$anonymous$$anager.LoadScene. So it gives an error.
I have a feeling that my problem may not be the code but has to do with that the colliding of the two objects doesn't work correctly.
I'll dive into that. Thanks anyway!
Answer by jonahsrocket · Aug 09, 2016 at 10:47 AM
I found the solution using a javascript code that i found here:
@jonahsrocket COOL!! Glad you found your answer and good luck with the rest of your project. $$anonymous$$e, I have been going back and forth between $$anonymous$$icrosoft`s Virtual Academy for (absolute beginner) C# and Video Studio. Also PixelNests and Ray Wenderlich`s site for Unity w/C# for game development. I have some HT$$anonymous$$L5 and Processing / Processing-JS knowledge. But this is all new to me, and a bit over whelmed. Though very excited at the same time. Will just have to endure the newbie growing pains and Practice, Practice and more Practice!!!
Good Luck to you with the rest of your works and ...Peace.....
@mrwmrutski Yes, it is pretty hard to get into this. I know a bit javascript and html5 also but I am a newbie with C#. I also experience the same you do, getting the right info is not allways easy. But I am happy that I did not give up. Every (tiny) step forward makes me happy that I am getting to where I want to be. But it's taking time (and a lot of patience).
On the more positive side: The forum here is very helpfull. I wouldn't know how to get forward without it.
You also good luck with getting to know Unity! Cheers!
Just offering this up as something to look at. I am not pushing their stuff but not to share these. As a possibility for you to get something from them would be selfish. The first is a Free and fully explained step by step video for learning and working with C#. He really takes his time explaining in detail every nut and bolt of it. The second is $$anonymous$$SDN for documentation on C#, many options and links. All Free and you can export them completely or by chapters to your drive. To study from off-line or where ever. Both are from $$anonymous$$icrosoft : Virtual Academy and $$anonymous$$icrosoft Development Network ($$anonymous$$SDN) It has been very help full and offer a community advice Q/A form.
Just a "do unto others thing I believe in" and without others help, I would be lost myself. Will stay in touch ..... now off to watch a video!! Console.Write("...Peace....."); Console.Read();
Your answer
![](https://koobas.hobune.stream/wayback/20220612090039im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a growing mesh 0 Answers
Failed to create agent because it is not close enough to the NavMesh 4 Answers
Compare Tag on Collision (Collider.CompareTag) 2 Answers
Need Help With Collider Script. 2 Answers