How to make auto-generate button unity c#

Here’s what i need to do:
1- I need to list out all the game object. (DONE)
2- The list should be clickable and it will popup the properties of the game object that’s been click and the image of that particular game object. (IM STUCK)

Question:
What should i do to make the list is clickable and it will popup?

What i’ve tried:
Making the list as a button. (auto-generate button)

Here are my codes:

//start
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class ListObject : MonoBehaviour {
GameObject[ ] allObjects;
string clickedlist = “”;

void Start ()
{
allObjects = UnityEngine.Object.FindObjectsOfType ();
Debug.Log ("Total Length: " + allObjects.Length.ToString ());
Debug.Log (“LIST OBJECT”);

}

void OnGUI()
{
// Make a background box
GUI.Box(new Rect(10,10,100,90), “Loader Menu”);

//for loop to get the list object
for(int x=0; x<allObjects.Length; x++)
{
clickedlist = allObjects[1];
if(GUI.Button(new Rect(20,40,80,20), clickedlist ))
{
//Here, are the function that will popup the properties of the object
}
}

}
} //end

if(GUI.Button(new Rect(20,40,80,20), clickedlist ))

this will be "cannot implicitly convert type " ‘UnityEngine.GameObject’ to ‘string’ “”

p/s: If u have other solution to what i actually want to do, please put comment below. Hope all your solutions and suggestions really really helps me. thank you so much!

SOLVED:

void OnGUI()
{
// Make a background box
GUI.Box(new Rect(10,10,100,90), “Loader Menu”);

//for loop to get the list object
for(int x=0; x<allObjects.Length; x++)
{
clickedlist = allObjects[1].name;
if(GUI.Button(new Rect(20,40,80,20), clickedlist ))
{
//Here, are the function that will popup the properties of the object
}
}

when allObjects contain a list of your gameobejcts, then you have to do clickedlist.name to get the name of the Gameboject that is a string

1 Like