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

Opening a scene on collision with a specific object?

Discussion in 'Scripting' started by Denisowator, Oct 25, 2015.

  1. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    So I have the premade "FPSController.prefab" on my scene, and I made a simple structure with a plane inside it. And I need a script that will check if the fpsController collides with the plane (which is called PiramidPortal), which will cause the scene to change to another one (which is called Empty).
    I don't know coding, but I know the basics and what some of the commands mean and do. Also don't feel obligated to explain stuff (that is outside the coding part) to me like I'm a child, I'll figure it out no problem.

    P.S. I tried a few sources for both C# and JS, but whatever I do, I either get a bunch of errors or the script will just not do anything.
     
  2. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
  3. UltiGamer835

    UltiGamer835

    Joined:
    Jun 5, 2013
    Posts:
    8
    To do this you will triggers to check the collision between the player and the plane. Simply turn on the "Is Trigger" option in the Inspector for your plane's collider, then call an action based on whether the trigger has been entered. The script has to be attached to your plane. Here's an example:
    Code (CSharp):
    1. //Checks to see if trigger was entered.
    2. void OnTriggerEnter(Collider other){
    3.      //Loads level based on the name.
    4.      Application.LoadLevel("LevelName");
    5.      //Make sure you go to your build settings and add the scene you want to load, if you don't, you'll get a null reference error. Press Ctrl + Shift + B to open the build settings.
    6. }
    I hope this helps! :D