Search Unity

If GUI.Button in other script has been pressed, then do something...

Discussion in 'Scripting' started by Slyrfecso1, Aug 31, 2015.

  1. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Hi,

    I have a lot of different C# Scripts with different GUI Buttons with Box & Labels.
    They are showing the menu.
    If I click on a Button, then that is opening a BOX & Label.
    I have named the Buttons from B1-B8.
    In every script the function of button is working with counter, therfore 1 = opened, 2 = closed.


    How can I check If one of them has been pressed, then the actual is closing?
    (If I click on all buttons, then all boxes are opening and this is wrong, because the boxes are layering each other and this is looking very nastily.)

    My idea, but it isn't working:

    If (B2-B8 pressed)
    {
    counterB1==2;
    }
     
  2. DrSnake

    DrSnake

    Joined:
    Oct 17, 2014
    Posts:
    33
    This is just assumptions since u didn't post the code:
    I take it all the buttons have one and the same script?
    In that case, every button has it's own instance of the script.
    to check the counter for button b3 for example you should find its GameObject and then its script instance by doing:
    GameObject.Find("buttonnamehere").GetComponent<ScriptName>().counter;

    if this isn't helping you, please post a bit more code, so we can evaluate.
     
  3. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Dear DrSnake,

    Thank you for your solution, but isn't helped.
    Please check my project then you will see the problem.
    (Click on all icons on the top one after another, you will see the layering problem.)
    http://terberendezo.alexbutor.hu
    user: a
    pass: a

    I got this error:
    alapterulet.counterAT' is inaccessible due to its protection level

    This is my "B6" users button script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class felhasznalo : MonoBehaviour {
    6.  
    7.     public GUIStyle userStyel;
    8.     public GUIStyle userTooltipSytle;
    9.     public GUIStyle szoveg;
    10.     int counterAT=0;
    11.  
    12. //    string email = "";
    13. //    string username = "";
    14. //    string password = "";
    15. //    string confirm = "";
    16.  
    17.     public enum Menu {
    18.         MainMenu,
    19.         NewGame,
    20.         Continue
    21.     }
    22.  
    23.     public Menu currentMenu;
    24.     public List<Game> prefabLista = new List<Game>();
    25.  
    26.  
    27.  
    28. //    public void AddTarget(Transform savedPrefabTransform){
    29. //        prefabLista.Add (new Game());  
    30. //      
    31. //    }
    32.  
    33.  
    34.  
    35.     void OnGUI () {
    36.      
    37.  
    38.         if (counterAT==2)
    39.         {
    40.             counterAT = counterAT-2;
    41.         }
    42.      
    43.         if (GUI.Button (new Rect (Screen.width/2+35, 10, 50, 48), "" ,userStyel))
    44.         {
    45.             counterAT++;
    46.             Debug.Log(counterAT);  
    47.         }
    48.      
    49.      
    50.         if (counterAT == 1)
    51.         {
    52.         GUI.Box (new Rect (Screen.width / 2 - 200, 80, 405, 400), ""  ,userTooltipSytle);
    53.             GUI.Label(new Rect(Screen.width / 2 - 170, 100, 60, 25), "FELHASZNÁLÓI FELÜLET" ,szoveg);
    54. ////            email = GUI.TextField (new Rect (Screen.width / 2 - 60, 135, 250, 25), email, 40);
    55. ////            username = GUI.TextField ( new Rect (Screen.width / 2 - 60, 170, 250, 25), username, 40);
    56. ////            password = GUI.PasswordField (new Rect (Screen.width / 2 - 60, 205, 250, 25), password, "*"[0], 40);
    57. ////            confirm = GUI.PasswordField (new Rect (Screen.width / 2 - 60, 240, 200, 25), confirm, "*"[0], 40);
    58. //
    59.             GUI.Label(new Rect(Screen.width / 2 - 170, 380, 60, 25), "Hamarosan elérhető!\n\n" +
    60.                      "- árkalkulációt készíthet\n" +
    61.                       "- megrendelését elküldheti\n" +
    62.                       "- felhasználói adatokat módosíthat" ,szoveg);
    63.  
    64. //            if (GameObject.Find("B1").GetComponent<alapterulet>().counterAT)
    65. //            {
    66. //                //counterAT=0;
    67. //            }
    68. //
    69. //
    70. ////            if (GUI.Button (new Rect (Screen.width / 2 - 60, 280, 100, 30), "Belépés")) {
    71. ////                Debug.Log(email + " " + username + " " + password + " " + confirm);
    72.  
    73.  
    74.  
    75.          
    76.         //--------------------------------
    77.  
    78.             GUILayout.BeginArea(new Rect(Screen.width / 2 -170,150,200, 300));
    79. //                GUILayout.BeginHorizontal();
    80. //                GUILayout.FlexibleSpace();
    81. //                GUILayout.BeginVertical();
    82. //                GUILayout.FlexibleSpace();
    83.              
    84.                 if(currentMenu == Menu.MainMenu) {
    85.                  
    86.                     GUILayout.Box("Térberendezés");
    87.                     GUILayout.Space(10);
    88.                  
    89.                     if(GUILayout.Button("ÚJ üres tér")) {
    90.                                                          
    91.                         foreach(GameObject AllgameObjectsDelete in GameObject.FindGameObjectsWithTag("Player"))
    92.                         {
    93.                         Destroy (AllgameObjectsDelete);
    94.                         }
    95.                         currentMenu = Menu.MainMenu;
    96.  
    97.  
    98.                     }
    99.  
    100.                     if(GUILayout.Button("Az összeállítás mentése")) {
    101.                         Game.current = new Game();
    102.                         currentMenu = Menu.NewGame;
    103.                     }
    104.                  
    105.                     if(GUILayout.Button("Korábbi betöltése")) {
    106.                         SaveLoad.Load();
    107.                         currentMenu = Menu.Continue;
    108.                     }
    109.  
    110. //                    if(GUILayout.Button("A lista törlése")) {
    111. //                        SaveLoad.Load();
    112. //                        Game gameToRemove = SaveLoad.savedGames.First<game>; //Replace with game you want to remove
    113. //                        if (gameToRemove != null)
    114. //                        {
    115. //                        SaveLoad.savedGames.Remove(gameToRemove);
    116. //                        }
    117. //                        SaveLoad.Save();
    118. //                        }
    119.  
    120.  
    121.                 }
    122.              
    123.                 else if (currentMenu == Menu.NewGame) {
    124.                  
    125.                     GUILayout.Box("Személyes adatok");
    126.                     GUILayout.Space(10);
    127.                  
    128.                     GUILayout.Label(("Az Ön neve"), szoveg);
    129.                     Game.current.vezetek.name = GUILayout.TextField(Game.current.vezetek.name, 20);
    130.                 GUILayout.Label(("Az összeállítás megnevezése"), szoveg);
    131.                     Game.current.osszeallitas.name = GUILayout.TextField(Game.current.osszeallitas.name, 20);
    132.  
    133.                  
    134.                     if(GUILayout.Button("Mentés")) {
    135.                         //Save the current Game as a new saved Game
    136.  
    137.      
    138.                      
    139.  
    140.                         GameObject[]go = GameObject.FindGameObjectsWithTag("Player");  
    141.                         foreach(GameObject AllgameObjectsSave in go)
    142.                             {
    143.                 //            prefabLista.Add (new Game(AllgameObjectsSave.name));
    144.                          
    145.                             }
    146. //                    foreach(GameObject AllgameObjectsSave in GameObject.FindGameObjectsWithTag("Player"))
    147. //                    {
    148.                     //    Game.current.savedPrefabName = AllgameObjectsSave.name;
    149.                     //    prefabLista.Add (new Game(AllgameObjectsSave.name));
    150. //                        prefabLista.Add (new Game(AllgameObjectsSave.transform));
    151.                 //        Game.current.savedPrefabName = AllgameObjectsSave.name;
    152. //                        prefabLista.Add (new Game());
    153. //                        Game.current.savedPrefabName.name = AllgameObjectsSave.name;
    154. //                        prefabLista.Add(sPName);
    155. //                        Debug.Log (AllgameObjectsSave.name);
    156.  
    157.  
    158.  
    159.  
    160. //
    161. //                    }
    162.  
    163.              
    164.  
    165.  
    166.                         SaveLoad.Save();
    167.                         //Move on to game...
    168.                         //Application.LoadLevel(1);
    169.                         currentMenu = Menu.MainMenu;
    170.                     }
    171.                  
    172.                     GUILayout.Space(10);
    173.                     if(GUILayout.Button("Vissza")) {
    174.                         currentMenu = Menu.MainMenu;
    175.                     }
    176.                  
    177.                 }
    178.              
    179.                 else if (currentMenu == Menu.Continue) {
    180.                  
    181.                     GUILayout.Box("Mentés kiválasztása");
    182.                     GUILayout.Space(10);
    183.                  
    184.                     foreach(Game g in SaveLoad.savedGames) {
    185.                         if(GUILayout.Button(g.vezetek.name + " - " + g.osszeallitas.name)) {
    186.                         foreach(GameObject AllgameObjectsDelete in GameObject.FindGameObjectsWithTag("Player"))
    187.                         {
    188.                             Destroy (AllgameObjectsDelete);
    189.                         }  
    190.                         Game.current = g;
    191.                     //    Instantiate (Resources.Load ("Prefabs/AS-A-8.6-F.E") as GameObject);
    192.                     //    Debug.Log (Game.current.savedPrefabName.name);
    193.                     //    Instantiate (Resources.Load ("Prefabs/"+Game.current.savedPrefabName.name));
    194.  
    195.                         //GameObject myObj = Instantiate (myObjects [whichItem]) as GameObject;
    196.                             //Move on to game...
    197.                             //Application.LoadLevel(1);
    198.                          
    199.                         }
    200.                      
    201.                     }
    202.                  
    203.                     GUILayout.Space(10);
    204.                     if(GUILayout.Button("Vissza")) {
    205.                         currentMenu = Menu.MainMenu;
    206.                     }
    207.                  
    208.                 }
    209.              
    210. //                GUILayout.FlexibleSpace();
    211. //                GUILayout.EndVertical();
    212. //                GUILayout.FlexibleSpace();
    213. //                GUILayout.EndHorizontal();
    214.                 GUILayout.EndArea();
    215.              
    216.             }
    217.  
    218.  
    219.  
    220.  
    221.  
    222.  
    223.  
    224.          
    225.          
    226.              
    227.      
    228.         }
    229.     }
    230.  
     
  4. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    That's because 'counterAt' is private. You cannot access it from the outside without Reflection. You have three options:
    1. Change the visibility of 'counterAt'
    2. Implement access methods (getters and setters)
    3. Use runtime reflection
     
  5. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    1. changed in alapterulet script: public int counterAT=0;
    2th & 3th I don't know, please help me, thanks.
     
  6. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    2:
    The reason why you use different visibilities (private, protected, internal, public) for your fields, classes, structs and enumerations is safety. You hide variables so they don't exist for scripts that aren't supposed to change them.
    If you make everything public, you're writing S***ty code that is prone to error.

    However, sometimes you want certain things to be readable from the outside. In that case you work with "Getters". A getter is a function that simply returns a value (or reference).
    Code (csharp):
    1. public class A {
    2.     private int number;
    3.  
    4.     //Getter for number
    5.     public int GetNumber() {
    6.         return number;
    7.     }
    8. }
    9.  
    10. public class B {
    11.     private void A_Function() {
    12.         A instance = new A();
    13.         Debug.Log(instance.number); // doesn't work! number is private and inaccessible for B!
    14.         Debug.Log(a.GetNumber()); // works! You can read number, but cannot change it!
    15. }
    A "Setter" is the same idea, but instead of returning a value (or reference) it changes it. They can be used to perform sanity checks of the values you're trying to assign.
    Code (csharp):
    1. private int neverGreaterThanEleven;
    2.  
    3. public void SetNeverGreaterThanEleven(int value) {
    4.     if(value > 11) {
    5.         neverGreaterThanEleven = 11;
    6.     }
    7.     else {
    8.         neverGreaterThanEleven = value;
    9.     }
    10. }
    There are also more elegant ways to create this sort of behavior in C#, but that's enough for a start. Wrap your head around this first. ;)

    3:
    Runtime reflection is an advanced feature. Don't bother looking into it, if you're still new to programming. It's powerful, but dangerous. It's not something to be used lightly in game development.
     
  7. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Thank you for your solutions. The getter function is good for me.
    I don't understand the small "a" letter in the last Debug.Log.

    I tried to check the number in A script, but I get this error:
    The name `a' does not exist in the current context

    Code (CSharp):
    1.  
    2. private void alapterulet_Function() {
    3. alapterulet instance = new alapterulet();
    4. Debug.Log(instance.counterAT); //doesn'twork! numberisprivateandinaccessibleforB!
    5. Debug.Log(a.GetNumber()); //works! Youcanreadnumber, butcannotchangeit!
    6.  
    7.  
    8. if (instance.counterAT == 1)
    9.  {
    10. counterFE = 0;
    11.  }
    12.  
    13. // or -------------------------
    14.  
    15.  
    16. if (a == 1)
    17. {
    18. counterFE = 0;
    19. }
    20.  
    21. }
    22.  
     
    Last edited: Sep 2, 2015
  8. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    You'd have to post more of your code. Where do you declare ("create") a?
    Code (csharp):
    1. Blah a = new Blah(); // for example
     
  9. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    I think, very small thing is missing in my knowledge and if you solve that my mind will lighting up:)


    First script: alapterulet // I need the counterAT

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Text.RegularExpressions;
    4.  
    5. public class alapterulet : MonoBehaviour {
    6. public int counterAT=0;
    7. public int GetNumber() {return counterAT;}
    8.  
    9. void OnGUI () {
    10.  
    11.  
    12.  
    13.                 if (counterAT==2)
    14.                         {
    15.                         counterAT = counterAT-2;
    16.                         }
    17.            
    18.         if (GUI.Button (new Rect (Screen.width/2-265, 10, 50, 48), "" ,alapteruletStyle))
    19.                 {
    20.                     counterAT++;
    21.                 Debug.Log("ButtonATpressed");
    22.                 }
    23.            
    24.            
    25.                 if (counterAT == 1)
    26.                 {
    27.                  //something happening...
    28.                 }
    29. }
    30.  


    Second script: felhasznalo // In this, if counterAT is changing, then change counterFE.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class felhasznalo : MonoBehaviour {
    6.  
    7.     public GUIStyle userStyel;
    8.     public GUIStyle userTooltipSytle;
    9.     public GUIStyle szoveg;
    10.     public int counterFE=0;
    11.  
    12. private void alapterulet_Function() {
    13.         alapterulet instance = new alapterulet();
    14.         Debug.Log(instance.counterAT); //doesn'twork! numberisprivateandinaccessibleforB!
    15.         Debug.Log(a.GetNumber()); //works! Youcanreadnumber, butcannotchangeit!
    16.    
    17.    
    18.         if (instance.counterAT == 1)
    19.         {
    20.             counterFE = 0;
    21.         }
    22.  
    23. void OnGUI () {
    24.    
    25.  
    26.         if (counterFE==2)
    27.         {
    28.             counterFE = counterFE-2;
    29.         }
    30.    
    31.  
    32.         if (GUI.Button (new Rect (Screen.width/2+35, 10, 50, 48), "" , userStyel))
    33.         {
    34.             counterFE++;
    35.             Debug.Log("ButtonFELpressed");
    36.         }
    37.    
    38.    
    39.         if (counterFE == 1)
    40.         {
    41.               //something happening...
    42.                }
    43. }
    44.  
    45.  
     
    Last edited: Sep 2, 2015
  10. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Hi,
    I have solved that on other way, it is not so elegant, but working well.
    I used the script in script call function.
    Every script are on the same object, therefore I can select them very easily in public declaration.


    Code (CSharp):
    1. public alapterulet caller1; // you need to select in the inspector the same object
    2. public Inventory caller3; // you need to select in the inspector the same object
    3. public ButorlapInventory caller4; // you need to select in the inspector the same object
    4. public FemfestekInventory caller5; // you need to select in the inspector the same object
    5. public felhasznalo caller6; // you need to select in the inspector the same object
    6.  
    7. ...
    8.  
    9. if (counterME == 1)
    10. {
    11. caller1.helloAT ();
    12. caller3.helloIN ();
    13. caller4.helloBU ();
    14. caller5.helloFEM ();
    15. caller6.helloFEL ();
    16. ...
    17.  
    18. }
    19.  
    20.  

    In "felhasznalo" script:
    Code (CSharp):
    1. public void helloFEL(){
    2.    
    3.         counterFE = 0;
    4.  
    5.     }

    If I add them in all script (lot of work), then will work what I want.
    If you have time, check again my project.
     
    Last edited: Sep 2, 2015
  11. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    One small thing: Use this instead:
    Code (csharp):
    1. [SerializeField]
    2. private alapterulet caller1,
    3. [SerializeField]
    4. private ...