Search Unity

Change screen when player touches GameObject

Discussion in 'Scripting' started by alexmyl, Apr 20, 2017.

  1. alexmyl

    alexmyl

    Joined:
    Apr 20, 2017
    Posts:
    3
    I would ask how to do the first person controler, if touches a GameObject, then the game automaticly change scene.
    Thank you in advance.
     
    Last edited: Apr 20, 2017
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Do you mean change the scene or change something on the screen? Or something else?
     
  3. alexmyl

    alexmyl

    Joined:
    Apr 20, 2017
    Posts:
    3
    Thank you for your answer! Sorry for the mistake of mirwriting scene as screen. Yes, as you said, I would ask how to do the first person controler, if touches a GameObject, then the game automaticly change scene.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  5. alexmyl

    alexmyl

    Joined:
    Apr 20, 2017
    Posts:
    3
    How about that?
    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    2. using UnityEngine;
    3.  
    4. public class Teleport : MonoBehaviour
    5. {
    6.     public class ExampleClass : MonoBehaviour
    7.     {
    8.         void OnTriggerEnter(Collider other)
    9.         {
    10.             SceneManager.LoadScene("Screen03", LoadSceneMode.Additive);
    11.         }
    12.  
    13.     }
    14. }
    And after writing the code, how I can run it to the object?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, you just want to put that script on the gameobject that you will run into.

    Note: If anything else can run into that object, you should do a quick check to compare the tag to ensure that it was the player that ran into [it] before loading the level. If nothing else can run into it (to trigger it), you don't need to do that. *also, if that's the case, I think you can remove the "Collider other" portion in the parameters, as well (slight performance boost). Lastly, if the script fails when you remove the Collider other part, just put it back and I've mixed that up with Collision lol.

    Try it out :)

    LoadSceneMode.Additive means you'll be keeping the current scene + adding a new one.. all good, if that's what you intended.