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

Problem with the tutorial Code ?

Discussion in 'Scripting' started by Quast, Oct 21, 2015.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I was following this tutorial
    http://www.thegamecontriver.com/2014/09/create-level-lock-unlock-system-unity-46.html
    every thing is fine except one error that pop up from no where :mad:


    Code (CSharp):
    1. [/B]
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class LevelSelectScript : MonoBehaviour {
    6.  
    7. private int worldIndex;
    8. private int levelIndex;
    9.  
    10. void  Start (){
    11. //loop thorugh all the worlds
    12. for(int i = 1; i <= LockLevel.worlds; i++){
    13. if(Application.loadedLevelName == "World"+i){
    14. worldIndex = i;
    15. CheckLockedLevels();
    16. }
    17. }
    18. }
    19.  
    20. //Level to load on button click. Will be used for Level button click event
    21. public void Selectlevel(string worldLevel){
    22. Application.LoadLevel("Level"+worldLevel); //load the level
    23. }
    24.  
    25. //uncomment the below code if you have a main menu scene to navigate to it on clicking escape when in World1 scene
    26. /*public void  Update (){
    27. if (Input.GetKeyDown(KeyCode.Escape) ){
    28. Application.LoadLevel("MainMenu");
    29. }
    30. }*/
    31. //function to check for the levels locked
    32. void  CheckLockedLevels (){
    33. //loop through the levels of a particular world
    34. for(int j = 1; j < LockLevel.levels; j++){
    35. levelIndex = (j+1);
    36. if((PlayerPrefs.GetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString()))==1){
    37. GameObject.Find("LockedLevel"+(j+1)).active = false;
    38. Debug.Log ("Unlocked");
    39. }
    40. }
    41. }
    42. }
    43. [B]
    So, How to fix it ?
     
    Last edited: Oct 21, 2015
  2. Okto247

    Okto247

    Joined:
    Jun 26, 2015
    Posts:
    7
    I think the error occours, cause the script can't find then "LockedLevel" with the right number.
    First, you have to look for the number he can't find. Try a "Debug.Log(j.ToString())" before line 8.
     
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Code (csharp):
    1. GameObject.Find("LockedLevel"+(j+1)).active = false;
    Didn't find anything. Debug what this does.

    With all these string dependencies you're bound to run into this sort of error.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Just looking at that code, I suspect that tutorial was written by someone in their second week of using Unity.... so many bad newbie habits, encouraged and enshrined in a tutorial. It's also out of date. .active is now deprecated, and .SetActive(false) should be used instead.

    But yes, Okto247 is correct. There should be a GameObject in your scene named LockedLevel1 (or some other number), and there isn't. It's possible the name was slightly mistyped in your scene or something along those lines.

    ....one of a million reasons GameObject.Find should never be used... I can walk through how to create a much better system than the one this tutorial is recommending if you'd like.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That's a weird way to code level selection. Suggest abandoning the tutorial and doing a different one.
     
    LaneFox and StarManta like this.