Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to get the default material of gameobject

Discussion in 'Scripting' started by FreshCoder-731319, Jun 5, 2020.

  1. FreshCoder-731319

    FreshCoder-731319

    Joined:
    May 21, 2020
    Posts:
    17
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ColourChange : MonoBehaviour
    7. {
    8.    public GameObject panel;
    9.    public GameObject cube;
    10.    public GameObject panelR;
    11.    public GameObject cubeR;
    12.  
    13.    public Color[] colors;
    14.    public Color[] colorsR;
    15.  
    16.  
    17.    public int whatColor = 1;
    18.    public int whatColorR = 1;
    19.  
    20.    void Update(){
    21.  
    22.  
    23.        for (int i = 0; i < colors.Length; i++){
    24.            if(i == whatColor){
    25.                cube.GetComponent<Renderer>().material.color = colors[i];
    26.            }
    27.  
    28.        }
    29.  
    30.        for (int i = 0; i < colorsR.Length; i++){
    31.            if(i == whatColorR){
    32.                cubeR.GetComponent<Renderer>().material.color = colorsR[i];
    33.            }
    34.  
    35.        }
    36.    }
    37.  
    38.    public void ChangePanelState(bool state){
    39.        panel.SetActive(state);
    40.    }
    41.  
    42.    public void ChangePanelRState(bool state){
    43.        panelR.SetActive(state);
    44.    }
    45.  
    46.  
    47.    public void ChangeCubeColor(int index){
    48.        whatColor = index;
    49.    }
    50.  
    51.    public void ChangeCubeRColor(int index){
    52.        whatColorR = index;
    53.    }
    54.  
    55. }
    56.  
    57.  
     

    Attached Files:

  2. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Code (CSharp):
    1. for (int i = 0; i < colors.Length; i++){
    2.            if(i == whatColor){
    3.               cube.GetComponent<Renderer>().material.color = colors[i];
    4.            }
    5.        }
    6.        for (int i = 0; i < colorsR.Length; i++){
    7.            if(i == whatColorR){
    8.                cubeR.GetComponent<Renderer>().material.color = colorsR[i];
    9.            }
    10.        }
    11.  
    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
    Code (CSharp):
    1. public void ChangeCubeColor(int index){
    2.   cube.GetComponent<Renderer>().material.color = colors[i];
    3.          
    4.    }
    5.  
     
    Last edited: Jun 5, 2020
    FreshCoder-731319 likes this.
  3. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    colors[index]
     
  4. FreshCoder-731319

    FreshCoder-731319

    Joined:
    May 21, 2020
    Posts:
    17
    Sorry trying understand your soloution, so should i get ride of my 2 for statement and replace it with

    Code (CSharp):
    1. public void ChangeCubeColor(int index){
    2.   cube.GetComponent<Renderer>().material.color = colors[index];
    3.        
    4.    }
    outside of update() ??
     
  5. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    You don't need anything in update
     
  6. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Where ever you call changecolor it will take the index you pass in and change color
     
  7. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    And yes get rid the 2 for loop
     
  8. FreshCoder-731319

    FreshCoder-731319

    Joined:
    May 21, 2020
    Posts:
    17
    Thank you so much! Worked perfectly :)