For Loop Problem
Hey guys, I think my problem is very easy but I'm dumb.
I need to make pick up script but now when I try to pick up something only name0 is active.
for (int i = 0; i < sticksMan.sticks.Length; i++)
{
GameObject foundStick = sticksMan.transform.FindChild(name + i).gameObject;
if (foundStick.activeInHierarchy == true)
{
//???
}
else if (foundStick.activeInHierarchy == false)
{
foundStick.SetActive(true);
Destroy(gameObject);
foundStick = sticksMan.transform.FindChild(name + i).gameObject;
}
break;
}
Hope you guys help!
Answer by JustinEllis · Mar 21, 2017 at 07:27 PM
Is your object named as follows? "ITEM 0" or "ITEM0". transform.FindChild(name + i) will look for an object like "ITEM0". If you want "ITEM 0" you need transform.FindChild(name + "" + i)
Also your break statement breaks you out of the loop after the first item did you mean continue? Not sure what youre using it for
Names of items was good. Actually solved that. That should be something like:
for (int i = 0; i < 5; i++)
{
GameObject foundStick= sticks$$anonymous$$an.transform.FindChild(name+ i).gameObject;
if (!foundStick.activeSelf)
{
foundStick.SetActive(true);
Destroy(gameObject);
break;
}
}
Answer by cjdev · Mar 21, 2017 at 09:01 PM
When you call Destroy(gameObject);
you're destroying the GameObject the script is attached to which might be why the rest of the script isn't run.
That's wasn't a problem :)
Actually solved.
for (int i = 0; i < 5; i++)
{
GameObject foundStick= sticks$$anonymous$$an.transform.FindChild(name+ i).gameObject;
if (!foundStick.activeSelf)
{
foundStick.SetActive(true);
Destroy(gameObject);
break;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612113644im_/https://answers.unity.com/themes/thub/images/avi.jpg)