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 : 

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

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 _= obj*.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 + "
");

GUI.Box(sizeBox, "Available Objects :
" + 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 = obj*.name;*

* StringBuilder allGameObjects = new StringBuilder();*
* for(int i = 0; i < objects.Length; i++)*
_ allGameObjects.Append(objects + "
");
_

* 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 :
" + gameObjectList);*

* }*
}