Search Unity

how do you change scenes through unity3d (in game)

Discussion in 'Scripting' started by hugotheholder, Feb 4, 2012.

  1. hugotheholder

    hugotheholder

    Joined:
    Nov 24, 2011
    Posts:
    8
    So I have been looking through unity questions and I have been trying to find a answer that actually works so my question is, I am trying to change scenes (in game) by clicking on an item example: a small plane, how would I do this?


    P.S thank you in advanced:)
     
  2. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
  3. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Have you gone to File > Build Settings... and Clicked the Button "Add Current" this will add the scene so that you can call it like in the link above. You would also have to open up the scene you want to load be loaded and add that to the settings as well (Add Current) then you would have something like

    Scenes/test.Unity 0
    Scene/LevelToLoad 1

    Code (csharp):
    1.  
    2. function OnMouseDown () {
    3.     Application.LoadLevel ("LevelToLoad");
    4.  
    if the above is not the issue and you want to know how to select the object you could use raycast (just one of other options)

    Code (csharp):
    1.  
    2. var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    3. if (Physics.Raycast (ray, 100)) {
    4.     print ("Hit something");
    5. }
    6. }
    7.  
    what this does is send a raycast from the main camera into the scene to your mouse input or cursor then you could load the level instead of printing hit something. You can also combine this with input.getbuttondown as well.

    If you use this method you'll also probably need to use layers so that dont raycast other objects and they all load your level.
     
    TrakJohnson likes this.
  4. hugotheholder

    hugotheholder

    Joined:
    Nov 24, 2011
    Posts:
    8
    ok Guys Im sorry but i might need more help, can someone tell me what to do with this OnMouseDown script and where to drag and drop it, also where to place the raycast scrip.
     
  5. pezz

    pezz

    Joined:
    Apr 29, 2011
    Posts:
    606
    Go try the unity tutorials.
    They actually help.
     
  6. hugotheholder

    hugotheholder

    Joined:
    Nov 24, 2011
    Posts:
    8
    which tutorial?
     
  7. pezz

    pezz

    Joined:
    Apr 29, 2011
    Posts:
    606
  8. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Sorry for the Delay but here you go kid! :)

    Attach this script to your camera, whats happening is var ray is shooting a ray from the camera (this case the Main camera) to the input.mousePosition (which is your cursor) and all of this happens in that first line and is always happening because of update function.

    then when you hit the fire button raycast (ray), then hit (like a hit point), then how far (100);

    If you look up raycast in the scripting reference you'll also see how to add a Draw command so that you can also see the ray in the scene for things when 100 is too far or maybe you'd need more.

    the last thing is to reference your hit (if hit.collider.name == whatever gameobject has this name) or you could also use .tag hit.gameObject.tag == whatever the tag name is

    then some more brackets to do more stuff inside of it like load your level or in this case print something to see if it works (lower left of the console editor) very very very helpful to use print and Debug.Log.

    Last tip once you attach this to your camera and hit play (first try clicking the sky or background you should see that nothing happens) then try clicking an object named Plane and presto you will see the print log in the console so its working. now that it works add in your load level stuff :)

    Im not sure if you'll find this in the Platform Tutorial so I figured Id help you out :) its all about research and a lot of googling which I myself do a lotttttttttttttttttttttttttttttttttttttt! and it seems is never ending and this is a great place to go when you've been searching for days and still cant figure it out :)

    Code (csharp):
    1. function Update()
    2. {
    3.     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    4.     var hit:RaycastHit;
    5.    
    6.     if(Input.GetButton("Fire1"))//GetMouseButton(0)
    7.     {
    8.         if(Physics.Raycast(ray, hit, 100))
    9.         {
    10.             if(hit.collider.name == "Plane")//name your plane Plane or change to whatever object you like
    11.             {
    12.                 //do your stuff here: like application load whatever level Sorry you gotta look this one up
    13.                
    14.                 //testing to see if it works (Debug.Log or print) lets use print this time
    15.                 print("Alright Level should now load :)");
    16.             }
    17.         }
    18.     }
    19.    
    20. }
     
    Last edited: Feb 8, 2012
    zachseven likes this.
  9. EDZVR

    EDZVR

    Joined:
    Jun 20, 2017
    Posts:
    2
    Ok I am getting a symbol error??
     
  10. EDZVR

    EDZVR

    Joined:
    Jun 20, 2017
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class M: MonoBehaviour
    {


    void OnMouseDown()




    {Application.LoadLevel("SomeLevel");
    }

    }

    function Update()
    {
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit:RaycastHit;

    if(Input.GetButton("Fire1"))//GetMouseButton(0)
    {
    if(Physics.Raycast(ray, hit, 100))
    {
    if(hit.collider.name == "Plane")//name your plane Plane or change to whatever object you like
    {
    //do your stuff here: like application load whatever level Sorry you gotta look this one up

    //testing to see if it works (Debug.Log or print) lets use print this time
    print("Alright Level should now load :)");
    }
    }
    }

    }
     
  11. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    1) Please do not necro threads
    2) Use code tags
    3) It appears your curley brackets are in the wrong place. Try this
    Code (CSharp):
    1. using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.  
    5.     public class M : MonoBehaviour
    6.     {
    7.  
    8.         void OnMouseDown()
    9.         {
    10.             Application.LoadLevel("SomeLevel");
    11.         }
    12.  
    13.         function Update()
    14.         {
    15.             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    16.             var hit:RaycastHit;
    17.  
    18.             if (Input.GetButton("Fire1"))//GetMouseButton(0)
    19.             {
    20.                 if (Physics.Raycast(ray, hit, 100))
    21.                 {
    22.                     if (hit.collider.name == "Plane")//name your plane Plane or change to whatever object you like
    23.                     {
    24.                         //do your stuff here: like application load whatever level Sorry you gotta look this one up
    25.  
    26.                         //testing to see if it works (Debug.Log or print) lets use print this time
    27.                         print("Alright Level should now load :)");
    28.                     }
    29.                 }
    30.             }
    31.  
    32.         }
    33.     }
     
  12. OwlTeaGames

    OwlTeaGames

    Joined:
    May 21, 2015
    Posts:
    11
    Here's how to change scenes in 2017!
     
  13. solruck

    solruck

    Joined:
    Nov 15, 2017
    Posts:
    1
    The correct way to do it in Unity 5.3+ is import the scenemanager like this:

    using UnityEngine.SceneManagement;

    then to switch to the scene you want is:
    SceneManager.LoadScene("Name of scene")

    this is in c#