Button script with scroll rect ??

Hi, sorry for my english…

i have this code

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ChangeMaterial : MonoBehaviour {
  
    public GameObject Obj3D; //object 3d
    public Material[] mats; //the materials
    private int index = 0;
    public Texture[] itemSlots; //texure of the button
    void Start () {
        Obj3D.GetComponent<Renderer>().material = mats[index];
                  }
   // Update is called once per frame
    public void OnGUI ()

    {
        //button size
        int buttonSizeX = 112;
        int buttonSizeY = 56;
        // number of pixels between the buttons
        int buttonSpacing = 1;
        // top-left position of the button grid
        int xOffset = Screen.width / 2- 300;
        int yOffset = Screen.height - 100;
        // number of columns
        int numCols = 20;
        int numButtons = 20; // Change this to itemSlots.Length

        Rect r = new Rect(0, 0, buttonSizeX, buttonSizeY); //create button
        for (int i = 0; i < numButtons; i++)
        {
            r.x = xOffset + (i % numCols) * (buttonSizeX + buttonSpacing); // column position
            r.y = yOffset + (int)Mathf.Floor((float)i / numCols) * (buttonSizeY + buttonSpacing); // row position

            if (GUI.Button(r,itemSlots[i])) // change i.ToString() to itemSlots[i].GetComponentInChildren().icon
            {
                index--;
                if (index < 0)
                {
                    index = mats.Length - 1;
                 
                }
                Obj3D.GetComponent<Renderer>().material = mats[index]; //assign materials for each button
        }
        }

    }

}

This code need for create a button for each material. The materials are 20. I have 20 button in horizontal.
I need that this button scrolling in x axis.
I’ m a newbie in scripting.I think that need create a scroll rect in this code for scrolling all button but how and where ?

tk for help!

This will be much easier (and in fact won’t require scripting at all) if you replace the deprecated old OnGUI code with the new UI system introduced in Unity 4.3.

the problem is that the buttons. I should create in script mode for assign for each button fast material and image of button…the number of the buttons is variable for model.

Nothing is hindering you to create a script that create the UGUI buttons at runtime.
UGUI in unperformant and outdated for ingame useage.
If you are unsure about where to start with the UGUI take a look in the unity leaning section.
There you can find great videos about the UI.
http://unity3d.com/learn/tutorials/topics/user-interface-ui

annoyingly the most relevant live training for this issue (being live training specifically about creating a scrollable list of buttons :hushed: ) is currently “forbidden” :frowning: :rage: