Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How would I fix this GUI script? (SOLVED)

Discussion in 'Scripting' started by LightWell, Sep 25, 2014.

  1. LightWell

    LightWell

    Joined:
    Sep 6, 2014
    Posts:
    43
    Code (JavaScript):
    1. #pragma strict
    2. var one = "Easy";
    3. var two = "Medium";
    4. var three = "Hard";
    5. var difval = 0;
    6. var difficulty = one;
    7. function Start () {
    8. difficulty = one;
    9. difval = 1;
    10. }
    11. function Update () {
    12. if(difval == 1) {
    13. difficulty = one;
    14. }
    15. if(difval == 2) {
    16. difficulty = two;
    17. }
    18. if(difval == 3) {
    19. difficulty = three;
    20. }
    21. difval = Mathf.Clamp(difval, 1, 3);
    22. }
    23.  
    24. function OnGUI () {
    25. var centeredStyle = GUI.skin.GetStyle("Label");
    26. centeredStyle.alignment = TextAnchor.UpperCenter;
    27. GUI.Box(Rect (Screen.width/2 - 50, Screen.height/3.5,90,24), "Main Menu");
    28. GUI.color = Color.blue;
    29. if(GUI.Button(Rect (Screen.width/2 - 50,Screen.height/3,90,24), "Level 1")) {
    30. Application.LoadLevel("lava1");
    31. }
    32. GUI.color = Color.white;
    33. GUI.Label(Rect (Screen.width/2 - 50, Screen.height/2,90,24), "Alpha V.001");
    34. if(difval == 1) {
    35. GUI.Box(Rect (Screen.width/2 - 50, Screen.height/2.5,90,24), difficulty);
    36. }
    37. if(difval == 2) {
    38. GUI.Box(Rect (Screen.width/2 - 50, Screen.height/2.5,90,24), difficulty);
    39. }
    40. if(difval == 3) {
    41. GUI.Box(Rect (Screen.width/2 - 50, Screen.height/2.5,90,24), difficulty);
    42. }
    43. if(GUI.Button(Rect (50 + Screen.width/2, Screen.height/2.5,24,24), ">>")) {
    44. difval += 1;
    45. }
    46. if(GUI.Button(Rect (Screen.width/2 - 85, Screen.height/2.5,24,24), "<<")) {
    47. difval -= 1;
    48. }
    49. Debug.Log("Current Difficulty:"+difficulty);
    50. }                                        
    This is what is happening when this code is ran:

    It says: " Easy " behind Medium, and any other difficulty that is selected, go ahead and run the script on yours, tell me if something different happens, really confused at the moment.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Your "if (difval)" statements are all doing the exactly same thing, so get rid of all that. Just make the GUI.Box with the difficulty string. Also put your difficulty strings in an array, so you can refer to them like "difficultyStrings[difval]". Get rid of the Update function entirely; separating logic out like that isn't a good idea plus there's no need to constantly evaluate that every frame. In the >> button code, make it be "difval = Mathf.Min (++difval, difficultyStrings.Length-1)", and in the << button code, make it be "difval = Mathf.Max (--difval, 0);"

    --Eric
     
    LightWell likes this.
  3. LightWell

    LightWell

    Joined:
    Sep 6, 2014
    Posts:
    43
    Sorry for the slow reply, I didn't have the if statements originally, I was trying to fix this issue with my script, anyways thanks for the reply, but could you explain why it's overlapping in the first place? I am sorry for asking, but I just would like to know where I went wrong so in the future it can be avoided.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Actually that's probably because you have the script running twice.

    --Eric
     
    LightWell likes this.
  5. LightWell

    LightWell

    Joined:
    Sep 6, 2014
    Posts:
    43
    Apparently, I misnamed one of my scene objects to " " or just nothing, so I had no idea it still existed, thank you so much for your help!