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

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:

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

public class ColourSlidertest : MonoBehaviour
{

    Color32[] Colors;
    float ColSlidervalue = 3f;  
   
    public void ColourSlider(float value)
    {
            ColSlidervalue = value;
    }

    void Awake()
    {
        Colors = new Color32[4];
        Colors[0] = new Color32 (0, 0, 255, 255);
        Colors[1] = new Color32(0,255,0,255);
        Colors[2] = new Color32(255,0,0,255);
        Colors[3] = new Color32(0,0,0,255);
    }


    void Update()
    {
        ColourSlider(this.GetComponent<Slider>().value);
       
            if (ColSlidervalue == 0f)
            {
                gameObject.GetComponent<Renderer>().material.color = Colors[0];
            }
            if (ColSlidervalue == 1f)
            {
                gameObject.GetComponent<Renderer>().material.color = Colors[1];
            }
            if (ColSlidervalue == 2f)
            {
                gameObject.GetComponent<Renderer>().material.color = Colors[2];
            }
            if (ColSlidervalue == 3f)
            {
                gameObject.GetComponent<Renderer>().material.color = Colors[3];
            }
    }
}

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??

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:

public void ColourSlider(float value)
{
    Debug.Log("Setting the color slider value to: " + value);
    ColSlidervalue = value;
}

The result of that Debug.Log will tell you tons about what’s going wrong.

1 Like

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?

Line 29 would be this one:

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() 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.

Clicking on the error in the console will also highlight the object that threw the exception.

Depending on what you’re trying to do Color.HSVtoRGB might be useful.

If the slider is not a component of the sphere that your “ColourSliderTest” script is attached to, 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:

using UnityEngine;
using UnityEngine.UI;

public class ColourSliderTest : MonoBehaviour
{
    Slider cSlider;
    Color32[] Colors;
    float ColSlidervalue = 3f;

    void Awake()
    {
        Colors = new Color32[4] { new Color32(0, 0, 255, 255),
                                  new Color32(0, 255, 0, 255),
                                  new Color32(255, 0, 0, 255),
                                  new Color32(0, 0, 0, 255) };
        /*use this and change the name in the quotes to whatever the slider's
        name is in the scene hierarchy if you decide not to assign the slider
        in the inspector*/
        //cSlider = GameObject.Find("HierarchyNameOfSlider").GetComponent<Slider>();
        cSlider.value = ColSlidervalue;
    }

    public void ChangeColour(float value)
    {
        if (value == 0f)
        {
            gameObject.GetComponent<Renderer>().material.color = Colors[0];
        }
        if (value == 1f)
        {
            gameObject.GetComponent<Renderer>().material.color = Colors[1];
        }
        if (value == 2f)
        {
            gameObject.GetComponent<Renderer>().material.color = Colors[2];
        }
        if (value == 3f)
        {
            gameObject.GetComponent<Renderer>().material.color = Colors[3];
        }
    }
}

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.

1 Like

Thank you so much!! This works perfectly!! Cheers :slight_smile:

Its not working for 3d models created with blender

Use this Script to work with the models created in Blender

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

public class Haircolor : MonoBehaviour {


    public Slider cslider;
    public Color[] Colors;
    float ColorSliderValue = 4f;
    public Material hair;

    public void Update(){

        if (cslider.value  < 1f)
        {
            hair.color = Colors [0];
        }else if (cslider.value < 2f)
        {
            hair.color = Colors [1];
        }else if (cslider.value < 3f)
        {
            hair.color = Colors [2];
        }else if(cslider.value < 4f)
        {
            hair.color = Colors [3];
        }
    }
}