- Home /
Find all objects that has specific tag
I want to find all the objects that are tagged as "LightUsers", and change their Light component's value "light.enabled" to false... I don't know how to find all of the objects tagged as specific tag... I think this is easy, but i don't get the stuff with arrays and that... I'm using C# for scripting.
Someone asked almost exactly the same question recently, again for C#. Do a search and you should find it with relative ease and have your exact answer to your question.
I tried to search for it, and the answers doesen't suit the thing i need
Post your code like Habsi70 says and we can see what might be the issue :)
Just fixed @767_2 's code, now it works... -
GameObject[] objs ;
objs = GameObject.FindGameObjectsWithTag("LightUser");
foreach(GameObject lightuser in objs) {
lightuser.light.enabled=false;
}
Answer by 767_2 · Nov 08, 2014 at 04:17 PM
find objects store them in array and get their light component
GameObject[] objs ;
objs = GameObject.FindGameObjectsWithTag("LightUsers");
foreach(gameObject lightuser in objs) {
lightuser.GetComponent<light>().enabled=false;
}
Error CS0118: 'UnityEngine.Component.gameObject' is a 'property' but is used like a 'type' (CS0118) (Assembly-CSharp)
Gives me that error, same goes with the answer from @habsi70 below when i try doing something with lightUsers
There were mistakes in the code and i've fixed them:
GameObject[] objs ;
objs = GameObject.FindGameObjectsWithTag("LightUser");
foreach(GameObject lightuser in objs) {
lightuser.light.enabled=false;
}
Answer by habsi70 · Nov 08, 2014 at 04:20 PM
GameObject.FindGameObjectsWithTag
You will get a List with all Objects with the provided Tag. Here some C# Code.
GameObject[] lightUsers;
lightUsers = GameObject.FindGameObjectsWithTag("LightUsers");
Error CS0118: 'UnityEngine.Component.gameObject' is a 'property' but is used like a 'type' (CS0118) (Assembly-CSharp)
Please post your Code. Did you write GameObject in uppercase? gameObject is not the same!
Just coped your code and then added code below it: lightUsers.GetComponent().enabled=false;
lightUsers.GetComponent().enabled=false;
This cannot work. lightUsers is an array of multiple Objects. Look at 767_2 s Code.
For GetComponent you must specify the type of component to return. Again look at 767_2 s Code.
I would suggest to read up on C# Arrays. For example http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
Also look at http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html
Please do not skip the basics, you will need them permanently. I know, it my feel like wasted time, but every $$anonymous$$ute you invest in getting up to speed with the basics will be worth it a thousand times. You only lose time, because later you will have to go there anyway.
OH I've found the problem, the problem in his code was gameObject should be GameObject, flase should be false, and GetComponent should just be light.enabled, it looks like this and it works perfectly:
GameObject[] objs ; objs = GameObject.FindGameObjectsWithTag("LightUser"); foreach(GameObject lightuser in objs) { lightuser.light.enabled=false; }
Your answer
Follow this Question
Related Questions
How do I find the closest target with a tag? c# 2 Answers
Distribute terrain in zones 3 Answers
Pick all gameobject with specific tag 1 Answer
how to use itween with the object attached to other moving object 0 Answers
Multiple Cars not working 1 Answer