- Home /
Question is off-topic or not relevant
I wanna use Gameobject.Find(string name, bool active)
i wonder why unity doesn't make this method?
why??? plz, tell me a reason.
There is GameObject.Find. So why do you even want something that you can do?
If you told us what you wanted to do and why, with specifics we could help you.
We should not need to guess what you wanted from your question.
Unity Answers is not a place to challenge Unity's design intentions or ask for features. You may be able to get a developers attention on the forums.
UA is for specific technical questions, not discussion of features.
Edit: If your question is "How can I find disabled GameObjects by name?" then ask that question.
Answer by fafase · Oct 14, 2014 at 06:26 AM
You can come up with your own:
public class Utility{
public static GameObject Find(string name, bool active = true){
if(String.IsNullOrEmpty(name))
{
return null;
}
var go = GameObject.Find(name);
if(go == null)
{
return null;
}
go.SetActive(active);
return go;
}
}
But you need to call it with :
GameObject obj = Utility.Find("name", true);
One reason I can see why this method does not do the active part is because it is called Find. The name should reflect the action it does. Actually, your method should FindAndActive to describe what it does. When you read code, you wrap parts of it into method so that it gets easier to read.
var go = GameObject.Find("name", false);
What is false? Is it the activation of the found object? Is it that you are looking only for inactive objects? Is it that you expect a particular output if the search is not successful...? You see that it gets confusing quickly.
Find just finds.
Answer by WTPS · Oct 14, 2014 at 08:14 AM
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Test : MonoBehaviour {
public Transform parent;
public bool isActive = false;
public string gameObjectName = "some gameobject";
public List<GameObject> foundGameObjects = new List<GameObject> ();
private Transform child = null;
// Use this for initialization
void Start ()
{
if (parent == null) {
parent = transform;
}
Debug.Log ("Searching inside " + parent.name);
foundGameObjects.Clear ();//clear list
GameObjectFind (gameObjectName, parent, isActive);
Debug.Log ("Found GameObjects (" + foundGameObjects.Count + ")");
}
public void GameObjectFind (string name, Transform parent, bool isActive)
{
if (string.IsNullOrEmpty (name)) {
return;
}
if (parent == null) {
return;
}
int childCount = parent.childCount;
for (int i = 0; i < childCount; i++) {
child = parent.GetChild (i);
if (child.name == name && child.gameObject.activeSelf == isActive) {
foundGameObjects.Add (child.gameObject);//add to list
}
if (child.childCount != 0) {
Debug.Log ("Searching inside " + child.name);
GameObjectFind (name, child, isActive);//recursive
}
}
}
}
public GameObject[] GameObjectFind (string name, Transform parent, bool isActive)
{
if (string.IsNullOrEmpty (name))
{
return;
}
if (parent == null)
{
return;
}
List<GameObject> list = new List<GameObject>();
foreach(Transform t in parent)
{
if(t.gameObject.activeSelf == isActive){
lit.Add(t.gameObject);
}
}
return list.ToArray();
}
But I don't think that is what he is after :).