Locate _MainTex from a public shader and assign to instantiated prefab for GUI
using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine;
public class Material_Selection : MonoBehaviour { public GameObject prefab; // This is the prefab used for each image displayed in the GUI public int NumberToCreate; // This is the number of images to create and display in the GUI - this should adapt per customisation option for the user 10 floors = 10 NumbersToCreate public Material[] materials; public int CurrentMaterial; GameObject FloorGUI;
void Start()
{
FloorGUI = GameObject.Find("Canvas_Floor"); // finds the gui
FloorGUI.SetActive(false); // turns the gui off
Populate(); // fills the gui optiontable
}
void Populate()
{
GameObject NewObject; //Create a Game Object instance for the prefab to duplicate per image
for (int i = 0; i < NumberToCreate; i++)
{
NewObject = (GameObject)Instantiate(prefab, transform); //Create new instances of our prefab until weve created as many as specified in NumberToCreate
NewObject.GetComponent<Image>().color = materials; // Randomises the colour of these images - will change to a texture swatch in a child/array
}
{
}
}
Answer by Dwakers92 · Feb 26, 2019 at 12:29 AM
I am trying to fill a GUI using the _MainTex from shaders created and put into a public array of materials. At the moment i can get it to instantiate a image prefab and randomise these colours from tutorials but id like to get it to source these images from materials plugged in instead.
Your answer
![](https://koobas.hobune.stream/wayback/20220612192500im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Dropdown Menu Not Closing 1 Answer
If instantiate prefab is selected how can i change the color? 1 Answer
Instantiate and destroy an object with the same key 1 Answer
problem with instantiated clones not running OnEnterTrigger / OnexitTrigger script? 0 Answers
Instantiating prefabs from an array of GameObjects 3 Answers