Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Make different object appear on the GUI toggle selection

Discussion in 'Scripting' started by fuhans1992, Aug 29, 2014.

  1. fuhans1992

    fuhans1992

    Joined:
    Jul 25, 2014
    Posts:
    4
    I am making the crafting system with the different choices by using Toggle GUI (Only one of the selection that can be selected). I was able to when the first selection has been chosen (clicked), the item appeared according to the database item id that I want it to appear. But, when I made the second selection (as the first selection is not chosen anymore), the item that from the first selection still there. I want it to change, but it does not. Here is the screenshots that shows the problem I encountered:

    31719-untitled-2.jpg 31718-untitled-1.jpg

    Here is the code:


    1. voidOnGUI()
    2. {
    3. // Set back the tooltip to empty, otherwise the previous hovering item will be still remains
    4. tooltip =string.Empty;
    5. // If the GUIHolder openedCraftingSystem is true
    6. if(GUIHolder.openedCraftingSystem)
    7. {
    8. // Draw the shop background before the crafting system itself
    9. GUI.DrawTexture(UserCraftingSystem.backgroundCraftingSystem,UserCraftingSystem.craftingSystemBackground);
    10. // Set the validation to false as user have not select any available option yet
    11. GUIHolder.validation =false;
    12. // Call the DrawCraftingSystem method
    13. DrawCraftingSystem();// This is just to show a crafting system window
    14. // Call the Craft Options function
    15. CraftOptions();
    16. // Call the Craft function
    17. Craft();
    18. // If the showTooltip is true, this boolean will be executed only if the shop is open
    19. if(showTooltip)
    20. {
    21. // Call the UserInformation SetGUI method and pass the value to it
    22. UserInformation.SetGUI(default(string),6,default(int));
    23. }
    24. }
    25. // If the craft screen is not opened
    26. else
    27. {
    28. // Call the SwitchToogle function
    29. SwitchToogle();
    30. }
    31. }
    32. voidAddItems(int id)
    33. {
    34. switch(id)
    35. {
    36. case1:
    37. UserCraftingSystem.AddItem(0);
    38. break;
    39. case2:
    40. UserCraftingSystem.AddItem(1);
    41. break;
    42. case3:
    43. UserCraftingSystem.AddItem(2);
    44. break;
    45. case4:
    46. UserCraftingSystem.AddItem(3);
    47. break;
    48. case5:
    49. UserCraftingSystem.AddItem(4);
    50. break;
    51. default:
    52. Debug.LogError("You have to choose either from 1 to 5!");
    53. break;
    54. }
    55. }
    56. voidCraftOptions()
    57. {
    58. if(GUI.Toggle(newRect(Screen.width -275,Screen.height -510,100,20), houseToogle,"House"))
    59. {
    60. houseToogle =SwitchToogle();
    61. AddItems(1);
    62. SetValidation(houseToogle);
    63. }
    64. if(GUI.Toggle(newRect(Screen.width -275,Screen.height -480,100,20), chairToogle,"Chair"))
    65. {
    66. chairToogle =SwitchToogle();
    67. AddItems(2);
    68. SetValidation(chairToogle);
    69. }
    70. if(GUI.Toggle(newRect(Screen.width -210,Screen.height -495,100,20), cupBoardToogle,"Cup Board"))
    71. {
    72. cupBoardToogle =SwitchToogle();
    73. AddItems(3);
    74. SetValidation(cupBoardToogle);
    75. }
    76. if(GUI.Toggle(newRect(Screen.width -125,Screen.height -510,100,20), tableToogle,"Table"))
    77. {
    78. tableToogle =SwitchToogle();
    79. AddItems(4);
    80. SetValidation(tableToogle);
    81. }
    82. if(GUI.Toggle(newRect(Screen.width -125,Screen.height -480,100,20), lampToogle,"Lamp"))
    83. {
    84. lampToogle =SwitchToogle();
    85. AddItems(5);
    86. SetValidation(lampToogle);
    87. }
    88. }
    89. boolSwitchToogle()
    90. {
    91. // Set the toggle buttons to false
    92. houseToogle = chairToogle = cupBoardToogle = tableToogle = lampToogle =false;
    93. returntrue;
    94. }
    95. voidSetValidation(bool toggle)
    96. {
    97. if(toggle)
    98. {
    99. GUIHolder.validation =true;
    100. }
    101. else
    102. {
    103. GUIHolder.validation =false;
    104. }
    105. }
    106. publicstaticvoidCraft()
    107. {
    108. craftRect =newRect(Screen.width -225,Screen.height -415,100,20);
    109. // Logic of enable and disable the selected GUI
    110. // Set the GUI.Button and GUI.enabled to be bool which is trigger to false if the game is paused
    111. GUI.enabled =GUIHolder.validation;
    112. GUIHolder.craftEnabled = GUI.Button(craftRect,"Craft");
    113. // End of Set the GUI.Button and the GUI.enabled to trigger the paused function
    114. // End of Logic
    115. }
    I apologize for posting too long. And really appreciate your help!