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. Dismiss Notice

How do I change colour of game object material with slider?

Discussion in 'Scripting' started by mdewit_geo, May 20, 2016.

  1. mdewit_geo

    mdewit_geo

    Joined:
    May 20, 2016
    Posts:
    5
    I'm extremely new to Unity, and I don't have much experience coding with C#, so I'd appreciate any help you can give! I'm using Unity v. 5.3.4, in case that matters...

    So what I'm trying to do is create a single slider which will change the colour of a game object material (a sphere) to certain preset colours when the slider is changed to certain values.

    I have this code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ColourSlidertest : MonoBehaviour
    6. {
    7.  
    8.     Color32[] Colors;
    9.     float ColSlidervalue = 3f;  
    10.    
    11.     public void ColourSlider(float value)
    12.     {
    13.             ColSlidervalue = value;
    14.     }
    15.  
    16.     void Awake()
    17.     {
    18.         Colors = new Color32[4];
    19.         Colors[0] = new Color32 (0, 0, 255, 255);
    20.         Colors[1] = new Color32(0,255,0,255);
    21.         Colors[2] = new Color32(255,0,0,255);
    22.         Colors[3] = new Color32(0,0,0,255);
    23.     }
    24.  
    25.  
    26.     void Update()
    27.     {
    28.         ColourSlider(this.GetComponent<Slider>().value);
    29.        
    30.             if (ColSlidervalue == 0f)
    31.             {
    32.                 gameObject.GetComponent<Renderer>().material.color = Colors[0];
    33.             }
    34.             if (ColSlidervalue == 1f)
    35.             {
    36.                 gameObject.GetComponent<Renderer>().material.color = Colors[1];
    37.             }
    38.             if (ColSlidervalue == 2f)
    39.             {
    40.                 gameObject.GetComponent<Renderer>().material.color = Colors[2];
    41.             }
    42.             if (ColSlidervalue == 3f)
    43.             {
    44.                 gameObject.GetComponent<Renderer>().material.color = Colors[3];
    45.             }
    46.     }
    47. }
    And I've attached it to the sphere, then in the On Value Changed section of the slider, I've called upon the ColourSlider function with ColSlidervalue on the game object (sphere). When I run the scene, the colour of the sphere doesn't change with the changing value of the slider. Any ideas as to what I might be doing wrong??
     
    Last edited: May 20, 2016
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    If you're new to Unity and coding, then the best advice is this:

    1: Figure out your assumptions.
    2: Put in Debug.Log statements that check those assumptions.

    Your assumptions here are:

    1: The slider's getting updated
    2: The slider value is ever exactly 1, 2 or 3
    3: It's the same slider you're working with
    4: The renderer is the one you're caring about.
    5: You have a material that cares about what color you give it.

    I've bolded the first one, because that's probably the one that's wrong. It's very easy to check; change your ColourSlider method to this:

    Code (csharp):
    1. public void ColourSlider(float value)
    2. {
    3.     Debug.Log("Setting the color slider value to: " + value);
    4.     ColSlidervalue = value;
    5. }
    The result of that Debug.Log will tell you tons about what's going wrong.
     
    LiterallyJeff likes this.
  3. mdewit_geo

    mdewit_geo

    Joined:
    May 20, 2016
    Posts:
    5

    Thank you for your quick reply. I tried this code again, with the debug log statement, and the value of the slider was exactly 1, however I also get an error saying 'NullReferenceException: Object reference not set to an instance of an object' for line 29. Do you have an idea how to fix this?
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    Line 29 would be this one:

    Code (csharp):
    1. ColourSlider(this.GetComponent<Slider>().value);
    You should (really, really) read up on nullreferences, but the tl;dr version means that you're trying to do something with an object that doesn't exist.

    The only thing that can cause that to happen on this script is that GetComponent<Slider>() isn't returning anything - which means that the script isn't on the same object as the slider.


    Note that this can also mean that you have a different instance of ColourSlidertest in the scene somewhere, which would be the one throwing the exception. If you search for ColourSlidertest in the bar on top of your hierarchy, it'll show all of the objects with that script on them.
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Clicking on the error in the console will also highlight the object that threw the exception.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
  7. Ahndrakhul

    Ahndrakhul

    Joined:
    Mar 7, 2015
    Posts:
    23
    If the slider is not a component of the sphere that your "ColourSliderTest" script is attached to,
    Code (CSharp):
    1. ColourSlider(this.GetComponent<Slider>().value);
    won't work. The "this" keyword also doesn't need to be used here. If your slider is the child of a canvas element in your scene hierarchy, you are probably looking for something like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class ColourSliderTest : MonoBehaviour
    5. {
    6.     Slider cSlider;
    7.     Color32[] Colors;
    8.     float ColSlidervalue = 3f;
    9.  
    10.     void Awake()
    11.     {
    12.         Colors = new Color32[4] { new Color32(0, 0, 255, 255),
    13.                                   new Color32(0, 255, 0, 255),
    14.                                   new Color32(255, 0, 0, 255),
    15.                                   new Color32(0, 0, 0, 255) };
    16.         /*use this and change the name in the quotes to whatever the slider's
    17.         name is in the scene hierarchy if you decide not to assign the slider
    18.         in the inspector*/
    19.         //cSlider = GameObject.Find("HierarchyNameOfSlider").GetComponent<Slider>();
    20.         cSlider.value = ColSlidervalue;
    21.     }
    22.  
    23.     public void ChangeColour(float value)
    24.     {
    25.         if (value == 0f)
    26.         {
    27.             gameObject.GetComponent<Renderer>().material.color = Colors[0];
    28.         }
    29.         if (value == 1f)
    30.         {
    31.             gameObject.GetComponent<Renderer>().material.color = Colors[1];
    32.         }
    33.         if (value == 2f)
    34.         {
    35.             gameObject.GetComponent<Renderer>().material.color = Colors[2];
    36.         }
    37.         if (value == 3f)
    38.         {
    39.             gameObject.GetComponent<Renderer>().material.color = Colors[3];
    40.         }
    41.     }
    42. }
    You can either drag the slider element onto the cSlider slot on the script, or use GameObject.Find() in the Awake()
    method.

    The slider settings would be,
    -MinValue = 0
    -MaxValue = 3
    -Whole Numbers = true
    Then, (as you've already done I think) you would drag the sphere with the ColourSliderTest script onto the slider's onvaluechanged(single) slot, and select the ChangeColour method that is directly under "Dynamic float" after
    hovering over "ColourSliderTest" in the component dropdown list.
     
    Last edited: May 20, 2016
    mdewit_geo likes this.
  8. mdewit_geo

    mdewit_geo

    Joined:
    May 20, 2016
    Posts:
    5

    Thank you so much!! This works perfectly!! Cheers :)
     
  9. priyanka2017

    priyanka2017

    Joined:
    Oct 5, 2017
    Posts:
    2
    Its not working for 3d models created with blender
     
  10. Tahair

    Tahair

    Joined:
    Nov 21, 2017
    Posts:
    1
    Use this Script to work with the models created in Blender
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Haircolor : MonoBehaviour {
    7.  
    8.  
    9.     public Slider cslider;
    10.     public Color[] Colors;
    11.     float ColorSliderValue = 4f;
    12.     public Material hair;
    13.  
    14.     public void Update(){
    15.  
    16.         if (cslider.value  < 1f)
    17.         {
    18.             hair.color = Colors [0];
    19.         }else if (cslider.value < 2f)
    20.         {
    21.             hair.color = Colors [1];
    22.         }else if (cslider.value < 3f)
    23.         {
    24.             hair.color = Colors [2];
    25.         }else if(cslider.value < 4f)
    26.         {
    27.             hair.color = Colors [3];
    28.         }
    29.     }
    30. }