number1.gameObject.SetActive (true);
number2.gameObject.SetActive (true);
number3 …
until number 50
Is there a solution in C# ?
number1.gameObject.SetActive (true);
number2.gameObject.SetActive (true);
number3 …
until number 50
Is there a solution in C# ?
You will want to move this to the scripting area of the forums, but you could do it like this:
public class SettingNames : MonoBehaviour
{
// Drag all the objects you want to name into this list in the inspector.
public List<GameObject> objectsToName = new List<GameObject>();
// The name you want all the gameObjects to have in the beginning.
public string namePrefix = "SOMENAME";
private void Start()
{
IncrementNames();
}
public void IncrementNames()
{
for(int i=0; i < objectsToName.Count; i++)
{
objectsToName[i].name = namePrefix + i.ToString();
}
}
}
Edit. I just realised. Do you want to name them? Or search for them by name? You can easily change my script so instead you’re doing GameObject.Find(namePrefix + i.ToString()).SetActive(true);, but that isn’t the most performant way to do things. You might find it easier to drag them into a list like I have here before the game starts, or if you’re instantiating them, keep a list of all the objects you’re instantiating.
I want to search them by name.
I tried with “GameObject.Find(namePrefix + i.ToString()).SetActive(true);”
but it didn’t work
public GameObject proba1;
...
public GameObject proba20;
GameObject proba;
public List<GameObject> objectsToName = new List<GameObject>();
public string namePrefix = "proba";
...
if (..........) {
proba1.gameObject.SetActive (true); //--> proba1 OK
int i=20;
GameObject.Find(namePrefix + i.ToString()).SetActive(true); // --> proba20 doesn't work
}
Right right I’m with you. Okay, proba1 in this example is the variable name, not the GameObject’s name. To find out the name of the GameObject you can do it like this:
Debug.Log(proba1.gameObject.name);
GameObject.Find(“SomeName”) is looking for the name of the GameObject in the hierarchy window in the inspector. So make sure the name of the GameObject in the hierarchy window matches the name you’re searching with GameObject.Find.
I watched the video and read the docs about GameObject.Find and I understand the way to use it.
But I don’t found the way to use it in my case.
In the hierarchy window in the inspector I have 50 gameobjects with name : proba1,proba2,… until proba50
so I would like that if i=6; → proba6.gameObject.SetActive (true);
but without doing 50 if{ }
I search to do a dynamic research with gameobject like this
if I=n; → proba(n).gameObject.SetActive (true)
Sorry for my bad English
I’d really suggest doing this in a different way. First off, you should rarely if ever use GameObject.Find, as it is a very expensive call. If you’re wanting to rename objects to make it easier for you to use GameObject.Find, I am certain you are using Find way too much in your code. If you have to use it, it should be just once like in a Start method when your scene is loading, and never from Update, for example.
What I would suggest is to throw all of these objects into a list or an array and reference them by index or loop through the list. You can make the list or array public so all your scripts can easily get access to it in place of using GameObject.Find. At worst case you would assign a tag to the gameobject that has the script that contains the list, and use FindWithTag to just find that object.
public List<GameObject> FiftyObjects;
public void SetAllActive()
{
foreach (GameObject ob in FiftyObjects)
{
ob.SetActive(true);
}
}
Thank you , you resolved the problem.
Foreach with list it’s that I needed