Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by zak666 · Jun 15, 2016 at 05:59 PM · scriptingbasics

C# find game object with tag and set active?

in c# how do you find a tame object with "TAG" and .SetActive(true?)

Comment
Add comment · Show 1
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 saud_ahmed020 · Jun 15, 2016 at 06:57 PM 0
Share

@zak666 Are you searching the following ??

https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

2 Replies

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

Answer by TBruce · Jun 15, 2016 at 08:30 PM

You can only find GameObjects that are currently active.

GameObject.FindWithTag() will return the first GameObject found that is active with the given tag.

Then there is GameObject.FindGameObjectsWithTag(). This returns an array of active GameObjects with the given tag if there are any to be found. If none exist an empty array will be returned.

I always use the following for GameObject.FindWithTag()

 GameObject go = null;
 if (GameObject.FindWithTag("MyTag") != null)
 {
     go = GameObject.FindWithTag("MyTag");
 }
 if (go != null)
 {
     // use it
 }

or for GameObject.FindGameObjectsWithTag()

 GameObject[] goArray = GameObject.FindGameObjectsWithTag("MyTag");
 if (goArray.Length > 0)
 {
     // this is an array, use it so
     GameObject go = goArray[0];

     // or
     for int (i = 0; i < goArray.Length; i++)
     {
         GameObject go = goArray[i];
     }
 }

or if you prefer to use a list

 using System.Collections.Generic;

 List<GameObject> goList = new List<GameObject>();
 goList = new List<GameObject>(GameObject.FindGameObjectsWithTag("MyTag"));
 if (goList.Count > 0)
 {
     // this is a list, use it so
     GameObject go = goList[0];

     // or
     for int (i = 0; i < goList.Count; i++)
     {
         GameObject go = goList[i];
     }
 }
Comment
Add comment · Show 1 · 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 Ali17X · Aug 10, 2020 at 07:31 PM 0
Share

TNNX allot

avatar image
-2

Answer by Shippety · Jun 15, 2016 at 06:55 PM

Use GameObject.FindWithTag ("TAG");

https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.htm

And then set that gameobject as active or inactive just like you mentioned-

 thisgameObject.SetActive (true);

https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

Comment
Add comment · Show 2 · 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 zak666 · Jun 15, 2016 at 07:54 PM 0
Share

No. If i found a answer on google search i wouldn't be here. error CS0029: Cannot implicitly convert type UnityEngine.GameObject[]' to UnityEngine.GameObject'

     ShipSelect = GameObject.FindGameObjectsWithTag ("ShipSelect");
                 ShipSelect.SetActive (true);
avatar image Shippety zak666 · Jun 16, 2016 at 02:11 PM 0
Share

$$anonymous$$kay, that's the difference between an array of gameobjects and a single gameobject -_- is ShipSelect an array? If it is, you need to specify which element, as in

 shipSelect[0].SetActive(true);

Otherwise, you want to use the singular form of findwithtag, not

 GameObject.FindGameObjectsWithTag

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

62 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 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

Basic Enemy follow script for Unity five, desperately needed! 0 Answers

error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `=' 1 Answer

How do I make a loop in a C# script? 1 Answer

Accessing script from another object 1 Answer

How to slowly rotate an object only once using scripting? 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