Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by elfasito · Jun 26, 2021 at 12:59 AM · scripting problemlistnewbieforeach

How to find text value in a gameobjects list

Hello people, I need a little help with my script syntax.

I have a list of gameobjects and I want to deactivate a button if the list already contain a gameobject with a text component with "X" value. if the list not have a "X" value, keep the button active.

this is my function so far:

 private void CheckingContentList() 
     {
         ActualSelectedDesc = Compare.text.ToString();
         foreach (GameObject child in SparePartsList)
         {
             if(SparePartsList.Count == 0)
             {
                 return;
             }
             else
             {
                 if (child.transform.Find("Text desc").gameObject.GetComponent<Text>().text == ActualSelectedDesc )
                 {
                     GetComponent<Button>().interactable = false;
                 }
                 else
                 {
                     GetComponent<Button>().interactable = true;                        
                 }
             }                
         }
 }


*The function "CheckingContentList" is repeated with a InvokeRepeating.

*"Compare", is the text value what I need to check if already exist in the list.

*Im using transform.Find because the gameobjects of the list have multiple text components


with only a gameobject in the list the function works fine. but when I add more gameobjects the button keep looping between enabled/disabled

PD: Im not sure if my explanation its clear, tell me if I need explain better.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SirCrazyNugget · Jun 26, 2021 at 02:27 AM 0
Share

There's a couple of things you need to change around and I'm if you could add a screenshot of the hierarchy it'd help answer it.

your if (SparePartsList.Count == 0) will never be true; you're already inside the elements within the list.

When you're using GetComponent() it's getting it for the component on the object this script resides, which I'm guessing you want instead for each SparePart? if not the last element will always choose whether it's interactable.

Without knowing the exact structure it's hard to code though try something like:

 foreach (var sparePart in SpareParts)
 {
     var text = sparePart.GetComponent<Text>();
     if (text != null && text.text == "X")
     {
         sparePart.GetComponent<Button>().interactable = false; //should do null check here unless you know it exists
     }
     else
     {
         sparePart.GetComponent<Button>().interactable = false; //should do null check here too unless you know it exists
     }
 
     //or short the if statement to
     //sparePart.GetComponent<Button>().interactable = text != null && text.text == "X";
 }


If you're trying to create it if ANY has "X" then place it in another function which returns true/false

 public bool TextContainsX()
 {
     foreach (var sparePart in spareParts)
     {
         if (sparePart.GetComponent<Text>().text == "X") //ugly but you get the point
             return true;
     }
 
     return false;
 }

//and to call it GetComponent().interactable = TextContainsX();

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by SalahChafai160 · Jun 26, 2021 at 02:43 AM

let me give you an example of what's happening when your code is executed, let's say your list has 4 elements:

1st element has a text component with a value of "hhozg"

2nd element has a text component with a value of "sdggl"

3rd element has a text component with a value of "X"

4th element has a text component with a value of "poijf"

so when looping through the list this is what happens:

1st element doesn't have a text component with a value of "X" so it enables the button

2nd element doesn't have a text component with a value of "X" so it enables the button

3rd element does have a text component with a value of "X" so it disables the button

4th element doesn't have a text component with a value of "X" so it enables the button

so to fix it you will need to stop looping once you find "X" as the value of a text component of one of the elements -----> add a break after disabling the button

this is how the check should be:

 if (child.transform.Find("Text desc").gameObject.GetComponent<Text>().text == ActualSelectedDesc )
 {
    GetComponent<Button>().interactable = false;
    break;
 }                 
 else
 {
    GetComponent<Button>().interactable = true;                        
 }


                    


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image elfasito · Jun 26, 2021 at 01:30 PM 0
Share

Hello, thats works perfect, thanks for the explanation. I could not solve the problem because I was not interpreting the syntax correctly. I appreciate you have taked time to explain it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

218 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

[Closed]Help with PhotonNetwork.playerList 2 Answers

from list of entries, select and add to scene beneath previous 1 Answer

Update list on mouse click 1 Answer

Help with Putting GameObjects into a list. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges