Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

load level on trigger enter

Discussion in 'Scripting' started by Nightmare Games, Nov 29, 2012.

  1. Nightmare Games

    Nightmare Games

    Joined:
    Sep 5, 2012
    Posts:
    201
    I just started trying to learn how to script and started off with javascript and really don't know how to make a script but i have decided to take a crack at making my first script:
    Code (csharp):
    1. var triggerEnter = false;
    2.  
    3. if triggerEnter = true();
    4. {
    5.     function OnTriggerEnter(gameObject : Transform)
    6.     {
    7.         Application.LoadLevel(levelToLoad);
    8.     }
    9. }
    10. else if triggerEnter = false
    11. {
    12.     do nothing
    13. }
    14.  
    in my game Im trying to have a death screen load quickly when you get inside the trigger

    Can you please explain to me how this can be done

    also if you know any websites or good videos where i can learn javascript for unity please tell me.
     
  2. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    Try attaching this script to the trigger:

    Code (csharp):
    1.  
    2. var theLevel : String;
    3.  
    4. function OnTriggerEnter (myTrigger : Collider) {
    5.  if(myTrigger.gameObject.name == "player"){
    6.  Application.LoadLevel(theLevel);
    7. }
    8. }
    9.  
    So just change "player" to your object that goes into the trigger, and in the inspector type your death screen scene name into the theLevel var.
     
  3. Nightmare Games

    Nightmare Games

    Joined:
    Sep 5, 2012
    Posts:
    201
  4. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    No problem.