Search Unity

Loading scene is not working

Discussion in 'Scripting' started by Llamabro4, Jun 20, 2019.

  1. Llamabro4

    Llamabro4

    Joined:
    Jun 13, 2018
    Posts:
    13
    Im making a level where when the player collides with a sprite with a box collider, the next scene loads. For whatever reason, the scene just won't load but it also wont error out either. I have both colliders set to trigger, the player has a rigidbody, I made sure to put the next level scene name into the string reference box provided on the gameobject's script, nothing shows up on the console, and both scenes are in build settings. If anyone has an idea of what is going on, your help is much appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class SceneMan : MonoBehaviour
    7. {
    8.     [SerializeField] private string newLevel;
    9.  
    10.     void OnTriggerEnter2d(Collider2D other)
    11.     {
    12.         if (other.CompareTag("Collider"))
    13.         {
    14.             SceneManager.LoadScene(newLevel);
    15.         }
    16.     }
    17. }
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    Debugs? Does the function run?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Last edited: Jun 21, 2019
  4. Skyho

    Skyho

    Joined:
    Apr 3, 2015
    Posts:
    10
    [SerializeField] private string newLevel; needs to be cast as an integer. Also make sure the integer represents a Build Setting index number of the correct scene wanting to load.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
  6. tashfiq103

    tashfiq103

    Joined:
    Mar 4, 2016
    Posts:
    15
    Before you call the "SceneManager.LoadScene("GameScene"), make sure you added the scene itself to the "buildSettings" to your project. Otherwise both the called by name and index won't work.

    Hope it solves the problem of yours ;)
     
    AndreiMarian likes this.
  7. ZylaksS

    ZylaksS

    Joined:
    Mar 14, 2018
    Posts:
    18
    You need use OnTriggerEnter2D(Collider2D other) method, but you use OnTriggerEnter2d(Collider2D other) method. You need big D at the end letter not small
     
  8. Llamabro4

    Llamabro4

    Joined:
    Jun 13, 2018
    Posts:
    13
    Alright thanks. It was just a stupid un capped d :/. Thanks for the help everyone!