- Home /
activating game objects with a tag
I have this code
if (turretBaseID==true)
{
tb1.active=true;
tb2.active=true;
tb3.active=true;
tb4.active=true;
tb5.active=true;
tb6.active=true;
}
but instead of the the many lines of .active=true, is it possible to have a line writing any gameobject with the tag tb.active=true?
Answer by e-bonneville · Mar 17, 2011 at 03:39 PM
Yes. Use a for loop like this:
In C#:
public GameObject[] tbObjects;
for (int i = 0;i<tbObjects.Length;i++) { tbObjects[i].active = true; }
In JS:
var tbObjects : GameObject[];
for(var i = 0;i<tbObjects.length;i++) { tbObjects[i].active = true; }
This is assuming that your tb
objects are references to GameObjects. If they're not, you can replace the array variable type with whatever object you are using. Populate the array with references to your tb
objects in the Inspector. If that's not an option, let me know and I'll try to come up with an alternate solution if I have time.
Please note that above script examples are untested and report any errors accordingly.
I don't a lot of experience with arrays so bare with me. In the inspector of the objec the script is attached to it shows Tb Objects with a drop down showing size = 0. how do I reference this to the tb objects?
You need to set the size to the amount of objects you have (in your case, set 0 to 6), then drag each object into a slot that matches its number-1. Because arrays are zero-based (start counting on 0), you'll want to match up tb1 with position 0, tb2 with position 1, tb3 with position 2, etc.
Thanks that has got it working and showing it as active, but when I try to get in as active = false it keeps showing as active.
Replace the tbObjects[i].active = true;
line with tbObjects[i].active = false;
and it should work. :)
good work my man, that's been causing me problems for weeks!! $$anonymous$$any thanks :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613113407im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to use if statement properly 1 Answer
Deactivate An Object After Certain Amount of Time 4 Answers
Does inactive Objects eat up Performance? 1 Answer
How do I change a gameobject to be active through a script? 1 Answer
How would I attached a gameobject to a prefab that is spawned at a specific time? 1 Answer