[ C# Scripting ] - Insert button into GridLayoutGroup

Hi.

I need to insert a button into GridLayoutGroup. How could I make it using C# ? What is it wrong ?

My Screen:

My Code:

for(int i = 0; i < 5; i++)
        {
            //Cast it as a Button, not a game object
            GameObject MyPanel = GameObject.Find ("MyPanelContent);
            Button newButton = (Button)Instantiate(buttonPrefab);
            newButton.transform.SetParent(MyPanel.transform,false);
            Debug.Log (i);
        }

Thank You

Do you have to create it dynamically, as opposed to setting it in the scene itself?

if you do, I’d suggest that you make a public GameObject prefab variable
as well as a public variable for “MyPanel” which you connect (drag & drop) in the inspector.

Then, your instantiate might be:

GameObject newButton = Instantiate(prefab);
newButton.transform.setParent(MyPanel.transform, false);

I do not think you need to convert it to a button, based on the code shown here.
Later, if you ever need it in code, you can get the button component …

ok, but I am trying and I have a error. I’m sorry. I’m a beginner and I would like to learn it.

setParent should be SetParent

Always double check caps

ok, it’s working, but I have a new error.

Thank You Again.

What is MyPanelContent? And you didn’t show the error.

Also worth noting that those lines can be condensed to one. There is an overload of Instantiate that takes the parent transform as an argument. Unity - Scripting API: Object.Instantiate

Instantiate(prefab, parentTransform);

Hi, ( MyPanelContent ) is my Panel

What’s the error, though? Is MyPanelContent null in your script? Does it even exist in the script?

Simply giving it the same name as how you named it in your hierarchy won’t make it exist (much less work) in your script.

You need to get a reference to it in the script. In a nutshell:

  1. Add “public Transform Content” to your class.
  2. A new “Content” field will appear in the Inspector. Drag your “MyPanelContent” panel onto this.
  3. Use “Content.transform” where needed in your script.

Note: I deliberately chose the name “Content” to make it clear we are talking about two things here: The script reference, and what you see in the inspector.

I just solve it. Look:
newButton.transform.SetParent(this.transform, false);

Thank You

1 Like

On the same object, you can even shorten it to “transform”. :slight_smile:
Glad you got it working.

Yeah, MyPanelContent was your gameobject name, but your code wouldn’t have been able to find that. Glad you figured it out.

I forget the ask a question, How could I alter the background image on my button with C# scripting ?

Thank You

If you want to change it because it’s highlighted or selected, there’s a built in way with “sprite swap” in the inspector.

If for anything else, you can access the Image component and alter the ‘sprite’ property. :slight_smile:

ok, but I’m a beginner,. How could I make it using that Script ?

for(int i = 0; i < 7; i++)
        {
            //Cast it as a Button, not a game object
            GameObject newButton = Instantiate(prefab);
            newButton.transform.SetParent(this.transform, false);
        }

Thank You

Like:

public Sprite mySprite; // outside of a method, assign in the inspector.

// then, inside the method where that code you posted is (after you make the button)
newButton.GetComponent<Image>().sprite = mySprite;

That’s it…Depends what you want. I mean, if you’re just changing the sprite once when it starts, you could make an array of 7 sprites and add spriteArray in that loop.
Whatever makes sense for you.

ok, I have a image called ( myImage.png ), so, It’s correctly ?

newButton.GetComponent<Image>().sprite = "myImage.png";

Thank You

It really seems like you’d be better served running through some of the material here: Unity Learn

Thank you, this really helped me. Without this, the object wasn’t found to set it as parent. This. actually took me a night of headache of finding it, refactoring code everywhere. The up side is: at least my code is much cleaner now.
Sometimes these small mistakes can take the most of your time in programming.