How to get the default material of gameobject

I am new to unity so forgive me if i get any technical words wrong…I have quickly made a simple code from my original to try make it easier to look at.

So my purpose is to change the color of my gameobject with the help of my color picker panel. However, once i hit the play button the two gameobjects (cube) goes to the the first index of my colour picker. What i want is the gameobject cubes to start of as the original materiel and only change color if the user selects what color they want from the color picker panel. How could i make that possible?

In the bigger picture ive got another model created with it own original color but once i hit the play button the model goes to the first color of the color array within the panel i made. For example, a character will have a red hate, white top and red shoes with a color array from panel created of green[0], grey[1], blue[2]. When i hit the play button the character colors will go all green instead of showing the default material colors.

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

public class ColourChange : MonoBehaviour
{
   public GameObject panel;
   public GameObject cube;
   public GameObject panelR;
   public GameObject cubeR;

   public Color[] colors;
   public Color[] colorsR;


   public int whatColor = 1;
   public int whatColorR = 1;

   void Update(){


       for (int i = 0; i < colors.Length; i++){
           if(i == whatColor){
               cube.GetComponent<Renderer>().material.color = colors[i];
           }

       }

       for (int i = 0; i < colorsR.Length; i++){
           if(i == whatColorR){
               cubeR.GetComponent<Renderer>().material.color = colorsR[i];
           }

       }
   }

   public void ChangePanelState(bool state){
       panel.SetActive(state);
   }

   public void ChangePanelRState(bool state){
       panelR.SetActive(state);
   }


   public void ChangeCubeColor(int index){
       whatColor = index;
   }

   public void ChangeCubeRColor(int index){
       whatColorR = index;
   }

}


for (int i = 0; i < colors.Length; i++){
           if(i == whatColor){
              cube.GetComponent<Renderer>().material.color = colors[i];
           }
       }
       for (int i = 0; i < colorsR.Length; i++){
           if(i == whatColorR){
               cubeR.GetComponent<Renderer>().material.color = colorsR[i];
           }
       }

These on update so it have change the colours because whatcolor is = 1 at start of the game you could just put it on here And you can get rid of what color color r since they all can use the same one.1 list will do the job

public void ChangeCubeColor(int index){
  cube.GetComponent<Renderer>().material.color = colors[i];
         
   }
1 Like

colors[index]

Sorry trying understand your soloution, so should i get ride of my 2 for statement and replace it with

public void ChangeCubeColor(int index){
  cube.GetComponent<Renderer>().material.color = colors[index];
        
   }

outside of update() ??

You don’t need anything in update

Where ever you call changecolor it will take the index you pass in and change color

And yes get rid the 2 for loop

Thank you so much! Worked perfectly :slight_smile: