- Home /
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?
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);
}
}
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?
Add `using System.Text` to the top of your script, and make sure that objects is declared like so: `string[] object;`
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?
I've updated my answer with the full script. That should clear up any confusion.
Your answer
Follow this Question
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