Reputation: 90
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
Upvotes: 1
Views: 3809
Reputation: 90862
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.
Upvotes: 5
Reputation: 599
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]);
}
}
}
Upvotes: 0