- Home /
Why won't the scene unlock?
So when the ball hits the platform, I want the unlockLevel int to equal the scene build index.
Here is my script that defines the int
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;
// Start is called before the first frame update
void Start()
{
SceneLoad = SceneManager.GetActiveScene().buildIndex;
Debug.Log(SceneLoad.ToString());
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Goal") {
SceneManager.LoadScene(newLevel);
}
if(SceneLoad == 4)
{
unlockLevel = 4;
Debug.Log("Next Level Unlocked");
}
if (SceneLoad == 5)
{
unlockLevel = 5;
}
if (SceneLoad == 6)
{
unlockLevel = 6;
}
if (SceneLoad == 7)
{
unlockLevel = 7;
}
if (SceneLoad == 8)
{
unlockLevel = 8;
}
if (SceneLoad == 9)
{
unlockLevel = 9;
}
if (SceneLoad == 10)
{
unlockLevel = 10;
}
if (SceneLoad == 11)
{
unlockLevel = 11;
}
if (SceneLoad == 12)
{
unlockLevel = 12;
}
if (SceneLoad == 13)
{
unlockLevel = 13;
}
if (SceneLoad == 14)
{
unlockLevel = 14;
}
if (SceneLoad == 15)
{
unlockLevel = 15;
}
if (SceneLoad == 16)
{
unlockLevel = 16;
}
if (SceneLoad == 17)
{
unlockLevel = 17;
}
}
}
Here is my script that unlocks the level
using System.Collections; using System.Collections.Generic; using UnityEditor.iOS; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class UnlockAll : MonoBehaviour { public Button button1; public Button button2; public Button button3; public Button button4; public Button button5; public Button button6; public Button button7; public Button button8; public Button button9; public Button button10; public Button button11; public InputField input; public Text text; public Button button12; public Button button13; public string answer; public ChangeScenes script; public InputField winText; public string win = "Demo Reached, More Levels Coming Soon!"; public Button[] levelButtons; public int sceneIndex;
void Start()
{
sceneIndex = SceneManager.GetActiveScene().buildIndex;
Debug.Log(sceneIndex);
}
void Update()
{
answer = input.text;
if ( answer == "UnlockAll" && Input.touchCount>0 || Input.GetKeyDown(KeyCode.Return))
{
button1.interactable = true;
button2.interactable = true;
button3.interactable = true;
button4.interactable = true;
button5.interactable = true;
button6.interactable = true;
button7.interactable = true;
button8.interactable = true;
button9.interactable = true;
button10.interactable = true;
button11.interactable = true;
button12.interactable = true;
button13.interactable = true;
Debug.Log("Levels Enabled");
}
if (ChangeScenes.unlockLevel == 4)
{
button1.interactable = true;
Debug.Log("It Works");
}
}
}
Answer by Luis_Gan · Apr 28, 2020 at 07:01 PM
There is many issues... Where is SceneManager.LoadScene(newLevel);
"newLevel" coming from? Why instead of if(SceneLoad == 4) { unlockLevel = 4; }
just unlockLevel = SceneManager.GetActiveScene().buildIndex;
and why did you change the scene before set those variables? The same happens with this button1.interactable = true;
just use a loop.
public InputField input;
public Button[] buttons;
string answer;
void Update()
{
answer = input.text;
if (answer == "UnlockAll" && Input.touchCount > 0 || Input.GetKeyDown(KeyCode.Return))
{
foreach (var item in buttons)
{
item.interactable = true;
}
Debug.Log("Levels Enabled");
}
buttons[ChangeScenes.unlockLevel - 1].interactable = true;
}
@$$anonymous$$ It works when I transition from my level to the level selection but not after I transition from level selection to another scene then back to level selection. Any suggestions and thanks for the help
Answer by ethanol3310 · Apr 30, 2020 at 05:12 AM
Can anyone else solve my problem. My new scripts are
the one that unlocks the level 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");
}
}
and the one 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;
// Start is called before the first frame update
void Start()
{
SceneLoad = SceneManager.GetActiveScene().buildIndex;
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);
}
}
}