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
3
Question by RickyX · Nov 08, 2014 at 04:08 PM · c#gameobjecttag

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.

Comment
Add comment · Show 4
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 MrSoad · Nov 08, 2014 at 04:11 PM 0
Share

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.

avatar image RickyX · Nov 08, 2014 at 05:09 PM 0
Share

I tried to search for it, and the answers doesen't suit the thing i need

avatar image MrSoad · Nov 08, 2014 at 05:25 PM 0
Share

Post your code like Habsi70 says and we can see what might be the issue :)

avatar image RickyX · Nov 08, 2014 at 05:49 PM 0
Share

Just fixed @767_2 's code, now it works... -

  GameObject[] objs ;
                 objs = GameObject.FindGameObjectsWithTag("LightUser");
                 foreach(GameObject lightuser in objs) {
                     lightuser.light.enabled=false;
                 }

2 Replies

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

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;
         }



Comment
Add comment · Show 3 · 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 RickyX · Nov 08, 2014 at 05:07 PM 0
Share

Error CS0118: 'UnityEngine.Component.gameObject' is a 'property' but is used like a 'type' (CS0118) (Assembly-CSharp)

avatar image RickyX · Nov 08, 2014 at 05:17 PM 0
Share

Gives me that error, same goes with the answer from @habsi70 below when i try doing something with lightUsers

avatar image RickyX · Nov 08, 2014 at 05:46 PM 3
Share

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;
             }
avatar image
3

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");
Comment
Add comment · Show 8 · 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 RickyX · Nov 08, 2014 at 05:07 PM 0
Share

Error CS0118: 'UnityEngine.Component.gameObject' is a 'property' but is used like a 'type' (CS0118) (Assembly-CSharp)

avatar image habsi70 · Nov 08, 2014 at 05:19 PM 0
Share

Please post your Code. Did you write GameObject in uppercase? gameObject is not the same!

avatar image RickyX · Nov 08, 2014 at 05:24 PM 0
Share

Just coped your code and then added code below it: lightUsers.GetComponent().enabled=false;

avatar image habsi70 · Nov 08, 2014 at 05:33 PM 0
Share
 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.

avatar image RickyX · Nov 08, 2014 at 05:45 PM 1
Share

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; }

Show more comments

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

31 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

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


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