Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

I am trying to make a script that will unlock levels after cleared but it doesn't work.

The script that changes scenes

using System.Collections;

using System.Collections.Generic;

using UnityEngine.SceneManagement;

using UnityEngine;


public class ChangeScenes : MonoBehaviour

{

  [SerializeField] private string newLevel;

  

  public static int unlockLevel = 0;

  public int SceneLoad;

  public int leveltoUnlock;



   


  // Start is called before the first frame update

  void Start()

  {

    SceneLoad = SceneManager.GetActiveScene().buildIndex;

    leveltoUnlock = PlayerPrefs.GetInt("levelPassed");

     

    Debug.Log(SceneLoad.ToString());

     

  }


  // Update is called once per frame

  void Update()

  {


    unlockLevel = SceneLoad;


     

  }

  void OnTriggerEnter(Collider col)

  {

    if (col.gameObject.tag == "Goal") {


       

      SceneManager.LoadScene(newLevel);


      

      

     

  }


    





  }

}








The script that unlocks levels:

using System.Collections;

using System.Collections.Generic;

using UnityEditor.iOS;

using UnityEngine;

using UnityEngine.SceneManagement;

using UnityEngine.UI;


public class UnlockAll : MonoBehaviour

{

  public Button[] buttons;

  public InputField input;

  public Text text;  

  public string answer;

  public ChangeScenes script;

  public InputField winText;

  public string win = "Demo Reached, More Levels Coming Soon!";

  void Start()

  {

    

  }

  void Update()

  {


    buttons[ChangeScenes.unlockLevel - 3].interactable = true;

    answer = input.text;


    if ( answer == "UnlockAll" && Input.touchCount>0 || Input.GetKeyDown(KeyCode.Return))

    {


       

      Debug.Log("Levels Enabled");

    }

     






    










  }

}




Thanks in advance and if u need anymore information comment.

Answers

  • Your "UnlockAll" cheat code thing does nothing but print a message.

    I also dont see anywhere in here where the "levelPassed" player pref gets set.

  • @Mouledoux

    It is a static int so it carries on through different scenes

  • @Mouledoux The unlockall was just for testing purposes

  • WarpWarp Administrator
    edited June 2020

    @Sauceror1 Static does not carry a value between scenes. Static means it is not relative to any object, and does not require instantiation to use.

    If you want values/objects to carry through scenes, use GameObject.DontDestroyOnLoad()

    Also, if possible, could you clear up some of the whitespace in the code you pasted? (big gaps between brackets) or put it all on pastebin and send us a link? It would be much easier to read (:

    Use pastebin, you dont need an account to make a paste :D

    If you need any clarification, im happy to help!

  • Static values do persist through scenes, as long as you dont override them. That still doesn't answer it though, just because it's static doesn't mean it's being set.


    Where are you setting the "levelPassed" player pref?

    Show me that code

  • WarpWarp Administrator

    @Mouledoux Thanks for the correction, didnt know that.

Sign In or Register to comment.