Search Unity

Change level on trigger

Discussion in '2D' started by JackFranklin96, Jan 26, 2015.

  1. JackFranklin96

    JackFranklin96

    Joined:
    Jan 21, 2015
    Posts:
    16
    I've added this code to try to change the level when the character enters the trigger but it isn't working for some reason. I've got a box collider set to is trigger and added a rigidbody.

    using UnityEngine;
    using System.Collections;
    public class Leveltrigger : MonoBehaviour {
    void OnTriggerEnter(Collider other) { Application.LoadLevel(2); } }

    Any help would be great Thanks
     
  2. U3DA

    U3DA

    Joined:
    Jan 23, 2015
    Posts:
    3
    Try to use Debug.Log(other.gameObject.name); and see if it returns the name of your Character.
    Also, I am sure you are aware that leves, are zero based. If you set the loading level to "2", the third level is going to be called.
     
    Last edited: Jan 26, 2015
  3. JackFranklin96

    JackFranklin96

    Joined:
    Jan 21, 2015
    Posts:
    16
    Hi

    I'm not sure how to use the Debug Log but are there any mistakes that are glaringly obvious that I'm not seeing as I'm very new to coding and I can't see what I'm doing wrong really.

    Thanks
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    2d or 3d game ? If you have colliders2D, then line should be
    void OnTriggerEnter2D (Collider2D other)

    and perhaps, it is better if trigger works only with player. Make tag of you player Player. The trigger itself dont need rigidbody2d, only player.

    using UnityEngine;
    using System.Collections;
    public class Leveltrigger : MonoBehaviour {
    void OnTriggerEnter2D (Collider2D other) {
    if (other.gameObject.tag == "Player")
    Application.LoadLevel(2); } }

    at last open build settings, and check, that your scene is added to scenes in build and look what number have the scene. I hope, you didnot forget add script to trigger.

    and if 3d, than i dont know :)
     
    Last edited: Jan 28, 2015
  5. Mikenseer

    Mikenseer

    Joined:
    Jul 24, 2012
    Posts:
    74
    Code (csharp):
    1. Application.LoadLevel("SceneName");
    This is a much safer way to call the scene you wish to load.