Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Scene Teleport

Discussion in 'Scripting' started by Legosdoctor, Aug 13, 2015.

  1. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    I need a script that when my player walks through a door, it will teleport them to a different scene, is there anywhere to start with this?
     
  2. Bradamante

    Bradamante

    Joined:
    Sep 27, 2012
    Posts:
    300
    You would put a collider on your player GameObject and mark it as a trigger. Then you would put a collider (propably with no visible geometry) behind the door. In your Build Settings you put your second level in the build list. You could then put a script like this on the door collider:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class LoadNewLevelOnContact : MonoBehaviour {
    6.  
    7.    // set as public variable in Editor
    8.    // usually 0 for first level
    9.    // 1 for target level
    10.  
    11.    public int levelIndexToLoad;
    12.  
    13.    public void OnTriggerEnter ( Collider cTrigger ) {
    14.  
    15.      // the Editor automatically offers the tag "Player" for GameObjects
    16.      // if the trigger object is not tagged as Player, ignore it
    17.    
    18.      if ( !cTrigger.tag == "Player" ) { return; }
    19.  
    20.      Application.LoadLevel ( levelIndexToLoad );
    21.    }
    22. }
    23.  
    Your question is very basic. Study the tutorial videos on the Unity YouTube channel and take a look at the Asset Store -> Unity Essentials -> Complete Projects.
     
    Last edited: Aug 13, 2015
  3. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    Thanks, i owe you one!
     
  4. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    Im getting a parsing error. do you need a copy of my script to see whats wrong with it?
     
  5. Bradamante

    Bradamante

    Joined:
    Sep 27, 2012
    Posts:
    300
    You can post the whole script if you want, sure ( in CODE tags please). Then again it's hard to judge without the context of your project.

    However, a parsing error should be visible in your code editor (like MonoDevelop) as a red marking, so you might be able to fix it there. Chose something like "Create CSharp Solution" from the menu (Cmd-K on Mac), then MonoDevelop will point to all kinds of errors.
     
  6. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    actually, i fixed it, but thanks anyway