Search Unity

(SOLVED) C# Falling Off Restarts The Scene

Discussion in 'Scripting' started by Nikola310, Dec 10, 2015.

Thread Status:
Not open for further replies.
  1. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    Hello,everyone!
    I've almost finished adding my fourth level to my game, but there's just one thing that I can't figure out how to do.
    I want the Scene to restart when the player falls off a plane (which is at the y axis of 0, so preferably the scene restarts when the Player is at y -30).
    I think the script should look something like this, but for some odd reason it doesn't work.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class DeathFall : MonoBehaviour {
    6.  
    7.     void DeathFallFunction()
    8.     {
    9.         if(gameObject.transform.position.y <= -30)
    10.         {
    11.             SceneManager.LoadScene(Application.loadedLevel);
    12.         }
    13.     }
    14. }
    Thank you in advance!
     
    k_win_11 and SimonBB22 like this.
  2. Fra123X

    Fra123X

    Joined:
    Mar 10, 2013
    Posts:
    40
    Try changing Application.loadedLevel with SceneManager.GetActiveScene ();
    Also, unless you are handling DeathFallFunction () somewhere else, you should change it to Update() or FixedUpdate ()
     
  3. alexisrabadan

    alexisrabadan

    Joined:
    Aug 26, 2014
    Posts:
    82
    I would set up a trigger in unity to detect when the player falls off the level, and handle the event with a generic trigger event class. Therefore, you would save having to tailor the code to any other future projects.
     
    Last edited: Dec 10, 2015
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    You are not checking it every frame call that method in Update.
     
  5. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    Oh,thanks. Simply putting that line of code in Update worked :)
     
  6. k_win_11

    k_win_11

    Joined:
    Nov 24, 2023
    Posts:
    1
    Thank you , it helped me as a beginner.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    So you know, you should use the "Like" button to show your appreciation. That way you don't necro an 8 year old thread for everyone.

    Thanks.
     
Thread Status:
Not open for further replies.