- Home /
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.
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();
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;
}
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
Follow this Question
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