1

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

2
  • Does your int OpenLevels need to be static? Also are there several instances of ButtonGatherer across the lifetime of the build? Commented May 5, 2020 at 9:07
  • Could you describe what Script fails to * Work Correctly * . means exactly? In both screenshots OpenLevels is not 0 and values seem to be set so what exactly is the issue? Commented May 5, 2020 at 14:08

2 Answers 2

5

Your issue probably lies in the line

OpenLevels = int.Parse(File.ReadAllText(Application.persistentDataPath + "Data.gokartgo"));}

There is a missing path separator so this results in a path looking like e.g.

                                     | This is now a file that doesn't even 
                                     | lie within your apps data!
                                     V 
\root\applications\yourapp\persistentDataData.gokartgo

On most devices such an access is not allowed!

You would need a / or \ between them.


But do not use + "/" see below!

Long story short: Never use + "" or + "/" for system file paths as they might be incorrect depending on the target platform (e.g. Windows \, Unix /).

Rather use Path.Combine which automatically inserts the correct path separator according to your platform:

OpenLevels = int.Parse(File.ReadAllText(Path.Combine(Application.persistentDataPath, "Data.gokartgo"));

If the issue you are talking about is the button numbers you are getting note that for FindGameObjectsWithTag it might not be guaranteed that the results are always returned in the same order.

Sign up to request clarification or add additional context in comments.

3 Comments

First of all,Thanks for help . Tried this But didn't work for me. Still the same problem :-(
does the according file exist on your device?
Yes, it existed. I fixed it. Actually The Sequence of buttons was wrong after I Build the file. As You said FindGameObjectWithTag not guaranteed the result in same order, I Fixed it by just adding Buttons Manually instead of using GameObject.FindGameObjectsWithTag("button") to get buttons, and it worked . Thanks
0

If your script works fine in the editor then the data file path might be the problem. Try this code and see if the problem still exists.

public class ButtonGatherer : MonoBehaviour
{
    public List<Button> buttons;
    public static int OpenLevels;
    void Start()
    {
        if (PlayerPrefs.HasKey("OpenLevels"))
        {
            OpenLevels = PlayerPrefs.GetInt("OpenLevels");
        }
        else
        {
            OpenLevels = 0;
            PlayerPrefs.SetInt("OpenLevels", 0)
        }

        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]);
        }
    }
}

1 Comment

Thanks For Help, This Too Didn't Work in my case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.