I am a Newbie in Unity. I recently created a script that disables buttons in a scene until a condition is satisfied (For a Level Selection Menu, to disable all levels until the player completes previous levels).
But the problem is - My script works fine when I run the game in Editor, but when I Build and then Run the Game, Script fails to * Work Correctly * .
Here is that script -
// Import required Modules
public class ButtonGatherer : MonoBehaviour
{
public List<Button> buttons;
public static int OpenLevels;
void Start()
{ if (OpenLevels == 0){
OpenLevels = int.Parse(File.ReadAllText(Application.persistentDataPath + "Data.gokartgo"));} //Reads Data from a file
GameObject[] btn = GameObject.FindGameObjectsWithTag("button");
for (int i = 0; i < btn.Length; i++){
buttons.Add(btn[i].GetComponent<Button>());
}
Debug.Log("Variable OpenLevels : "+OpenLevels);
for(int i = OpenLevels; i < buttons.Count; i++) {
buttons[i].interactable = false;
Debug.Log("Var i : " + i +", So Locked Level : " + buttons[i]); //Used This To debug and get values for variable i
}
}
}
I Have Also attached of the Screenshot of Debug Message That I Get -
When Running in Editor -
ScreenShot of Debug Trace when running in Editor
When Running After Building (Development Build) -
Screenshot of Debug Trace When Running After Building
Please Help ....
Edit : I Searched Unity Documentations and Forums but found Nothing
int OpenLevelsneed to be static? Also are there several instances of ButtonGatherer across the lifetime of the build?Script fails to * Work Correctly * .means exactly? In both screenshotsOpenLevelsis not0and values seem to be set so what exactly is the issue?