Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Amphet · Nov 30, 2012 at 08:58 PM · gameobjectvariablestring

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 ?

Comment
Add comment · Show 3
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 Landern · Nov 30, 2012 at 09:16 PM 0
Share

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"?

avatar image fafase · Nov 30, 2012 at 09:53 PM 0
Share

You could try to set the string before:

 string str = "/ImageTarget/obj" + currObjNum;
 currObj = GameObject.Find(str);
avatar image Amphet · Dec 01, 2012 at 05:20 PM 0
Share

@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.

2 Replies

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

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.

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 Amphet · Dec 02, 2012 at 07:35 PM 0
Share

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 !

avatar image
0

Answer by fajoaq · Dec 01, 2012 at 09:04 PM

currObjString = "ImageTarget/obj" + currObjNum.ToString(); // must convert int to string

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 fafase · Dec 02, 2012 at 09:08 AM 1
Share

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

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

15 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

Related Questions

GameObject to Variable to GameObject and add string? 1 Answer

GetComponent() - Is it possible to pass a string variable as name of the script? 3 Answers

GameObject.name to string 2 Answers

How do I assign a prefab reference to a private variable? 1 Answer

Edit Variables From Game! 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