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 Timurov · Oct 31, 2012 at 09:12 AM · c#guisceneobjectsgui.box

how to listing all game objects in GUI.Box?

Hey Guys! I am trying to do the listing by name of all available game objects in my scene and actually its almost done, but something is going wrong, that's why i need your help guys!

here is a code snipped in c#:

 using UnityEngine;
 using System.Collections;
 
 public class testLIST : MonoBehaviour {
     
     public string objects;
          
     void Start() {
         object[] obj = GameObject.FindSceneObjectsOfType(typeof (GameObject));
      
         foreach (object o in obj) 
         {
             
           GameObject g = (GameObject) o;
          objects = g.name;
             print(g.name);
         }
         
     }
     
     void OnGUI() {
         int i =0; 
         GUIStyle style = new GUIStyle();
         i = i+1;
         Rect sizeBox = new Rect(Screen.width/2-1, Screen.height/2-150, 300, 100);
         GUI.Box(sizeBox,"Available Objects : \n" + i +". " +objects.ToString(),style);
     }
     

When a code snipped is running, GUI.Box just represents the last object in the scene instead of all of them. What am I doing wrong?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by karl_ · Oct 31, 2012 at 01:50 PM

First issue is that `objects` should be a string array; the declaration looks like this:

 public string[] objects;

Next, if you're looking for GameObjects in scene, cast them as such. This line:

 object[] obj = GameObject.FindSceneObjectsOfType(typeof (GameObject));

should look like this:

 GameObject[] obj = GameObject.FindSceneObjectsOfType(typeof (GameObject)) as GameObject[];

Then, in your `Start` method, the foreach loop should assign the name to the proper index within that string. I think it's easier to just use a for loop. Something like this:

 // Set length of string array to match the length of obj array.
 objects = new string[obj.Length];
 for(int i = 0; i < obj.Length; i++)
 {
     objects[i] = obj[i].name;
 }

Now that you have an array containing the name of all your gameObject's names, iterate through that list in your OnGUI function. You'll need to repaint each label every frame, and it looks like you had a decent start there. Just add a for loop instead of trying to initialize `i` every time OnGUI is called (it will always be 1 in your example code).

 void OnGUI()
 {
     Rect sizeBox = new Rect(Screen.width/2-1, Screen.height/2-150, 300, 100);
 
     // Calling ToString() on an array object won't format all members, so build 
     // our own string and format it ourselves.  We use StringBuilder instead of
     // string += "" because StringBuilder is much faster when concatenating lots
     // of strings.  That, or move this to the Start() method and only call it once.
     // In fact, you should probably do that anyways.
     StringBuilder allGameObjects = new StringBuilder();
     for(int i = 0; i < objects.Length; i++)
         allGameObjects.Append(objects[i] + "\n");

     GUI.Box(sizeBox, "Available Objects : \n" + allGameObjects.ToString());
 }


And here is everything put together.

 using UnityEngine;
 using System.Collections;
 using System.Text;
 
 public class SceneObjects : MonoBehaviour {
 
     string gameObjectList;
 
     public void Start()
     {
         GameObject[] obj = GameObject.FindSceneObjectsOfType(typeof (GameObject)) as GameObject[];
         string[] objects;
 
 
         objects = new string[obj.Length];
         for(int i = 0; i < obj.Length; i++)
             objects[i] = obj[i].name;
 
         StringBuilder allGameObjects = new StringBuilder();
         for(int i = 0; i < objects.Length; i++)
             allGameObjects.Append(objects[i] + "\n");
 
         gameObjectList = allGameObjects.ToString();
     }
 
     void OnGUI()
     {
         Rect sizeBox = new Rect(Screen.width/2-1, Screen.height/2-150, 300, 100);
 
         // Calling ToString() on an array object won't format all members, so build 
         // our own string and format it ourselves.  We use StringBuilder instead of
         // string += "" because StringBuilder is much faster when concatenating lots
         // of strings.  That, or move this to the Start() method and only call it once.
         // In fact, you should probably do that anyways.
 
         GUI.Box(sizeBox, "Available Objects : \n" + gameObjectList);
     }
 }
Comment
Add comment · Show 4 · 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 Timurov · Oct 31, 2012 at 02:11 PM 0
Share

your suggestion produces 3 errors: Assets/testLIST.cs(12,17): error CS0029: Cannot implicitly convert type `string[]' to `string' Assets/testLIST.cs(15,25): error CS0200: The read only property or indexer `string.this[int]' cannot be assigned to Assets/testLIST.cs(27,17): error CS0246: The type or namespace name `StringBuilder' could not be found. Are you missing a using directive or an assembly reference?

avatar image karl_ · Oct 31, 2012 at 05:45 PM 0
Share

Add `using System.Text` to the top of your script, and make sure that objects is declared like so: `string[] object;`

avatar image Timurov · Nov 01, 2012 at 07:49 AM 0
Share

ohh... it shows another errors: Assets/testLIST.cs(26,61): error CS0841: A local variable `i' cannot be used before it is declared; Assets/testLIST.cs(26,21): error CS1502: The best overloaded method match for `UnityEngine.GUI.Box(UnityEngine.Rect, string)' has some invalid arguments; Assets/testLIST.cs(26,21): error CS1503: Argument `#2' cannot convert `object' expression to type `string'; what is wrong here i cant understand?

avatar image karl_ · Nov 01, 2012 at 03:07 PM 0
Share

I've updated my answer with the full script. That should clear up any confusion.

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

10 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

Related Questions

Best way to spawn new objects that arent already in the scene? 1 Answer

Display multiple scenes? 1 Answer

Changing GUI.Box opacity 3 Answers

Distribute terrain in zones 3 Answers

Add GUI elements to Scene View? 3 Answers


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