Search Unity

Building connecting levels.

Discussion in 'Scripting' started by Cous, Nov 28, 2015.

  1. Cous

    Cous

    Joined:
    Apr 17, 2015
    Posts:
    36
    Hi all.

    I'm trying to progress from one scene to the next. Only problem is I keep getting the error.
    The next level collision is a capsule collider, I have got interaction with player and capsule because I've got it set up for the capsule to disappear when they collide. This interaction can be turned on or off with out any problems (true/false thing)

    Any help, thoughts insight?
    greatly appreciated.
     

    Attached Files:

  2. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    :) Open up the scene you try to transition to in the editor, then press File>Build Settings and in the window for scenes press "Add Current" ( or whatever it says, can't quite recall. )

    The error is simply that Unity can't find the scene you try to open since it's not in the list of available scenes.
     
  3. Cous

    Cous

    Joined:
    Apr 17, 2015
    Posts:
    36
    Hi Kona,

    I have done this, and it sort of works... before it didn't work, then I added a string to my nextLevel script and it goes to the next level, but it does this instantly now :/ any thoughts on how to get so it only when collision?

    My Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewLevel : MonoBehaviour
    5. {
    6.  
    7.     public PlayerController controller;
    8.     public string Level_2;
    9.  
    10.     // Use this for initialization
    11.     void Start()
    12.     {
    13.         controller = (PlayerController)transform.GetComponent("PlayerController");
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.  
    19.         Application.LoadLevel(Level_2);
    20.     }
    21. }
    22.    

    It will only go to next level if (Level_2) is in the string field. If its not, it just comes up with error.
     

    Attached Files:

  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Well that script occurs on 'Update'. It'll happen the first time it updates.

    Instead wait for something event.

    For example if that script was on a GameObject with a BoxCollider attached flagged as a trigger. You could listen for the 'OnTriggerEnter', and test if the collider who entered is the player, and then load if true.
     
  5. Cous

    Cous

    Joined:
    Apr 17, 2015
    Posts:
    36
    I have a onTrigger event on my PlayerControlller script. and the capsule collider is tagged as 'istrigger' Would the trigger need to be moved?

    This is my playerController script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.  
    7.     public float speed;
    8.  
    9.     private Rigidbody rb;
    10.  
    11.     void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.     }
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         float moveHorizontal = Input.GetAxis("Horizontal");
    19.         float moveVertical = Input.GetAxis("Vertical");
    20.  
    21.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    22.  
    23.  
    24.         rb.AddForce(movement * speed);
    25.     }
    26.  
    27.     void OnTriggerEnter(Collider other)
    28.     {
    29.         if (other.gameObject.CompareTag("Rock"))
    30.         {
    31.             other.gameObject.SetActive(true);
    32.         }
    33.  
    34.         if (other.gameObject.CompareTag("End"))
    35.         {
    36.             other.gameObject.SetActive(true);
    37.         }
    38.     }
    39. }
    40.  
     
  6. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    Don't use the player collider as a trigger, instead make the object that should trigger a level change to a trigger, and try

    Code (CSharp):
    1. public void OnTriggerEnter( Collider other ) {
    2.      if( other.CompareTag( "Player" )) {
    3.           ChangeScene();
    4.      }
    5. }
    6.  
    7. public void ChangeScene() {
    8.      Application.LoadLevel( "levelName" );
    9. }
    Also do note that game objects that are disabled will not be able to call triggers, since all components in its hierarchy are also disabled, that includes their colliders.
     
    Cous likes this.
  7. Cous

    Cous

    Joined:
    Apr 17, 2015
    Posts:
    36
    Ahh! Thank you so much! That makes so much more sense! :) My levels are now linked.