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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Problem using tags in C#

Discussion in 'Scripting' started by shayan242, Apr 26, 2020.

  1. shayan242

    shayan242

    Joined:
    Jul 14, 2019
    Posts:
    6
    This part of my code doesn't run properly and when choice changes from it's primary state to another all the buttons disappear
    Code (CSharp):
    1.  
    2.     void FlagCheck()
    3.     {
    4.    
    5.         if (state.Choice == 1)
    6.         {
    7.             Flag = ("Mono Choice");
    8.             FlagOn();
    9.        
    10.             Flag = ("Dual Choice");
    11.             FlagDown();
    12.             Flag = ("Triple Choice");
    13.             FlagDown();
    14.        
    15.         }
    16.         else if (state.Choice == 2)
    17.         {
    18.             Flag = ("Mono Choice");
    19.             FlagDown();
    20.             Flag = ("Dual Choice");
    21.             FlagOn();
    22.             Flag = ("Triple Choice");
    23.             FlagDown();
    24.        
    25.          
    26.         }
    27.         else if (state.Choice == 3)
    28.         {
    29.             Flag = ("Mono Choice");
    30.             FlagDown();
    31.             Flag = ("Dual Choice");
    32.             FlagDown();
    33.             Flag = ("Triple Choice");
    34.             FlagOn();
    35.          
    36.         }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    This is not nearly enough code to tell you what you problem is. It's enough to suggest to me that you don't know what a function parameter is I guess, but we have no earthly idea what this code is supposed to do. (At a bare minimum, what are FlagDown() and FlagOn()? )
     
    shayan242 likes this.
  3. shayan242

    shayan242

    Joined:
    Jul 14, 2019
    Posts:
    6
    Sorry but when i write the whole code it consider it spam :(
    Flag down is supposed to turn of the objects with certain tags
    and FlagOn does the opposite
    Code (CSharp):
    1.  
    2.     void FlagDown()
    3.     {
    4.         GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag(Flag);
    5.         foreach (GameObject go in gameObjectArray)
    6.         {
    7.             go.SetActive(false);
    8.         }
    9.     }
    10.     void FlagOn()
    11.     {
    12.         GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag(Flag);
    13.         foreach (GameObject go in gameObjectArray)
    14.         {
    15.             go.SetActive(true);
    16.         }
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Most likely you have tags set wrong on your objects.

    If there is a script/logic issue, your code is so bizarre that it may be obfuscating the logic problem, so if it's not a simple "the object tags are set wrong" issue, then the following will help clean up and reduce your code to make it easier to make it more consistent. Simplest improvement would be to take advantage of parameters:
    Code (csharp):
    1. void SetFlag(string tag, bool objectsOn) {
    2.    GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag(tag);
    3.         foreach (GameObject go in gameObjectArray)
    4.         {
    5.             go.SetActive(objectsOn);
    6.         }
    7.     }
    8. //in your other code
    9. SetFlag("Mono Choice", false);
    It may seem picky, but reducing complexity like this will make your code run more consistently and make it easier to find logic errors. If you have copied and pasted code everywhere with one change, then it's really easy for inconsistent behavior to sneak in which is hell to debug.

    Plus, if your code is shorter, it's more likely you can paste the complete thing without tripping the spam filter. I've never seen the spam filter tripped before based just on the length of a comment, so I'm gonna take a stab in the dark and guess that this is a GIANT, omnibus script file with everything in one class because you don't know how to reference objects between different classes, and with lots of copied and pasted code because you don't know how to make functions more versatile/reusable.... would that be accurate?
     
    matkoniecz, shayan242 and bobisgod234 like this.
  5. shayan242

    shayan242

    Joined:
    Jul 14, 2019
    Posts:
    6
    Thanks for your help i changed that part
    i think the reason is when i turn an object off FindGameObjectsWithTag can't find them to set them on again i tried using an array to store game objects before turning them off but the result was worse it doesn't even work in first state
    this is for practice so i wrote most of the code except flagdown myself
    I wrote this at the start of class
    Code (CSharp):
    1.     GameObject[] goamono = GameObject.FindGameObjectsWithTag("Mono Choice");
    2.     GameObject[] goadual = GameObject.FindGameObjectsWithTag("Dual Choice");
    3.     GameObject[] goatriple = GameObject.FindGameObjectsWithTag("Triple Choice");

    and here is new part about Tags


    Code (CSharp):
    1. void SetFlag(GameObject[] ga, bool objectsOn)
    2.     {
    3.        
    4.         foreach (GameObject go in ga)
    5.         {
    6.             go.SetActive(objectsOn);
    7.         }
    8.     }
    9.  
    10.     void FlagCheck()
    11.     {
    12.      
    13.         if (state.Choice == 1)
    14.         {
    15.  
    16.             SetFlag(goamono, true);
    17.  
    18.             SetFlag(goadual, false);
    19.  
    20.             SetFlag(goatriple, false);
    21.          
    22.         }
    23.         else if (state.Choice == 2)
    24.         {
    25.             SetFlag(goamono, false);
    26.  
    27.             SetFlag(goadual, true);
    28.  
    29.             SetFlag(goatriple, false);
    30.          
    31.            
    32.         }
    33.         else if (state.Choice == 3)
    34.         {
    35.  
    36.             SetFlag(goamono, false);
    37.  
    38.  
    39.             SetFlag(goadual, false);
    40.  
    41.  
    42.             SetFlag(goatriple, true);
    43.            
    44.         }
    45.     }
     
  6. shayan242

    shayan242

    Joined:
    Jul 14, 2019
    Posts:
    6
    I fixed it with moving the object out of scene instead of turning it off :)
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Yeah, that's an issue with any variety of Find() - it only finds active objects. Your attempted solution there was close to correct, except that you need to have the Find() calls in the Start() function.
     
    shayan242 likes this.