Unity GUI not in build, but works fine in editor
Posted
by
Darren
on Game Development
See other posts from Game Development
or by Darren
Published on 2013-10-21T18:57:42Z
Indexed on
2013/10/21
22:05 UTC
Read the original article
Hit count: 351
I have:
- GUITexture attached to an object
- A script that has GUIStyles created for the Textfield and Buttons that are created in OnGUI(). This script is attached to the same object in number 1
- 3 GUIText objects each separate from the above.
- A script that enables the GUITexture and the script in number 1 and 2 respectively
This is how it is supposed to work: When I cross the finish line, number 4 script enables number 1 GUITexture component and number 2 script component. The script component uses one of number 3's GUIText objects to show you your best lap time, and also makes a GUI.Textfield for name entry and 2 GUI.Buttons for "Submit" and "Skip". If you hit "Submit" the script will submit the time. No matter which button you press, The remaining 2 GUIText objects from number 3 will show you the top 10 best times.
For some reason, when I run it in editor, everything works 100%, but when I'm in different kinds of builds, the results vary.
When I am in a webplayer, The GUITexture and the textfield and buttons appear, but the textfield and buttons are plain and have no evidence of GUIStyles. When I click one of the buttons, the score gets submitted but I do not get the fastest times showing.
When I am in a standalone build, the GUITexture shows up, but nothing else does. If I remove the GUIStyle parameter of the GUI.Textfield and GUI.Button, they show up.
Why am I getting these variations and how can I fix it?
Code below:
void Start ()
{
Names.text = "";
Times.text = "";
YourBestTime.text = "Your Best Lap: " + bestTime + "\nEnter your name:";
//StartCoroutine(GetTimes("Test"));
}
void Update()
{
if (!ShowButtons && !GettingTimes)
{
StartCoroutine(GetTimes());
GettingTimes = true;
}
}
IEnumerator GetTimes ()
{
Debug.Log("Getting times");
YourBestTime.text = "Loading Best Lap Times";
WWW times_get = new WWW(GetTimesUrl);
yield return times_get;
WWW names_get = new WWW(GetNamesUrl);
yield return names_get;
if(times_get.error != null || names_get.error != null)
{
print("There was an error retrieiving the data: " + names_get.error + times_get.error);
}
else
{
Times.text = times_get.text;
Names.text = names_get.text;
YourBestTime.text = "Your Best Lap: " + bestTime;
}
}
IEnumerator PostLapTime (string Name, string LapTime)
{
string hash= MD5.Md5Sum(Name + LapTime + secretKey);
string bestTime_url = SubmitTimeUrl + "&Name=" + WWW.EscapeURL(Name) + "&LapTime=" + LapTime + "&hash=" + hash;
Debug.Log (bestTime_url);
// Post the URL to the site and create a download object to get the result.
WWW hs_post = new WWW(bestTime_url);
//label = "Submitting...";
yield return hs_post; // Wait until the download is done
if (hs_post.error != null)
{
print("There was an error posting the lap time: " + hs_post.error);
//label = "Error: " + hs_post.error;
//show = false;
}
else
{
Debug.Log("Posted: " + hs_post.text);
ShowButtons = false;
PostingTime = false;
}
}
void OnGUI()
{
if (ShowButtons)
{
//makes text box
nameString = GUI.TextField( new Rect((Screen.width/2)-111, (Screen.height/2)-130, 222, 25), nameString, 20, TextboxStyle);
if (GUI.Button( new Rect( (Screen.width/2-74.0f), (Screen.height/2)- 90, 64, 32), "Submit", ButtonStyle))
{
//SUBMIT TIME
if (nameString == "")
{
nameString = "Player";
}
if (!PostingTime)
{
StartCoroutine(PostLapTime(nameString, bestTime));
PostingTime = true;
}
}
else if (GUI.Button( new Rect( (Screen.width/2+10.0f), (Screen.height/2)- 90, 64, 32), "Skip", ButtonStyle))
{
ShowButtons = false;
}
}
}
}
© Game Development or respective owner