C# find game object with tag and set active?
in c# how do you find a tame object with "TAG" and .SetActive(true?)
@zak666 Are you searching the following ??
https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
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];
}
}
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
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);
$$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
![](https://koobas.hobune.stream/wayback/20220612083011im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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