- Home /
How can I use GameObject.Find with a string and a variable ?
Hello, I need help with a project I'm working on. It's simple but I can't make it work as I am a complete newbie at programming. I would like to show objects one by one by clicking on two buttons Next/Previous. I named my object obj0, obj1, obj2... and they are children of an object named ImageTarget. Here is the code I wrote:
using UnityEngine; using System.Collections;
public class Buttons : MonoBehaviour{
public Texture btnPrev;
public Texture btnNext;
public int maxObjNum = 1;
public GameObject currObj;
public int currObjNum = 0;
void OnGUI() {
if (GUI.Button(new Rect(10,(Screen.height/2)-64,128,128),btnPrev)) {
currObj.SetActiveRecursively(false);
}
if (GUI.Button(new Rect((Screen.width-138),(Screen.height/2)-64,128,128),btnNext)) {
currObj.SetActiveRecursively(false);
currObjNum = currObjNum + 1;
if (currObjNum > maxObjNum) {
currObjNum = 0;
}
currObj = GameObject.Find("/ImageTarget/obj" + currObjNum);
currObj.SetActiveRecursively(true);
}
}
}
The GameObject.Find doesn't work, how can I fix this ?
There isn't anything wrong with what you did there, but since this is almost a duplication of http://answers.unity3d.com/questions/356942/how-to-find-a-gameobject-with-a-stringvariable-.html
again, your object structure...
So in your scene, you have a root object called "ImageTarget" and a child of that object is "obj"?
You could try to set the string before:
string str = "/ImageTarget/obj" + currObjNum;
currObj = GameObject.Find(str);
@Landern: Sorry for the duplicate, the block that shows code didn't work in the previous post. Yes, the object ImageTarget holds almost all the objects in my scene (I use Qualcomm's Vuforia AR plugin). The children are named obj0, obj1, obj2...
@fafase: Thanks for the tip but it didn't work, here is the new code:
using UnityEngine;
using System.Collections;
public class Buttons : $$anonymous$$onoBehaviour{
public Texture btnPrev;
public Texture btnNext;
public int maxObjNum = 1;
public GameObject currObj;
public int currObjNum = 0;
string currObjString ="";
void OnGUI() {
if (GUI.Button(new Rect(10,(Screen.height/2)-64,128,128),btnPrev)) {
currObj.SetActiveRecursively(false);
}
if (GUI.Button(new Rect((Screen.width-138),(Screen.height/2)-64,128,128),btnNext)) {
currObj.SetActiveRecursively(false);
currObjNum = currObjNum + 1;
if (currObjNum > maxObjNum) {
currObjNum = 0;
}
currObjString = "ImageTarget/obj" + currObjNum;
print (currObjString);
currObj = GameObject.Find(currObjString);
currObj.SetActiveRecursively(true);
}
}
}
The print(); returns a NullReferenceException error.
Answer by fafase · Dec 02, 2012 at 09:02 AM
This is what I have working:
public int maxObjNum = 1;
public GameObject currObj;
public int currObjNum = 0;
string currObjString ="";
void OnGUI() {
if (GUI.Button(new Rect((Screen.width-138),(Screen.height/2)-64,128,128),"Click")) {
if (++currObjNum > maxObjNum)currObjNum = 0;
currObjString = "Cube" + currObjNum;
print (currObjString);
currObj = GameObject.Find(currObjString);
print (currObj.name);
Destroy (currObj);
}
}
I have two cubes called Cube0 and Cube1 and I do see them being destroyed.
Could it be you are trying to find an inactive object? Find does not see inactive objects.
I would go and create a dictionnary of object so that you can play with them even though they are inactive.
Oh this is it ! The object I look for is indeed inactive at start, sorry I didn't mention it before ! I'll look into creating a dictionary. Thanks a lot !
Answer by fajoaq · Dec 01, 2012 at 09:04 PM
currObjString = "ImageTarget/obj" + currObjNum.ToString(); // must convert int to string
Actually, the compiler does it for you. The + sign indicates an overload of the operator that converts the object co$$anonymous$$g next to a string.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084745im_/https://answers.unity.com/themes/thub/images/avi.jpg)