Button color won't change.

I just typed out this question after hours of trying to figure this out, only to have the website ditch my question when I signed in to complete the post, so you can image I’m pretty frustrated by now.

All I want to do is change the color of my button using JS. I’ve looked at over 50 results on Google, and nothing answers my question.

I’ve tried a ton of different things, some compile and don’t work, others don’t compile at all.

My button is a GameObject Button, and it is a child of a Canvas object. The button is a toggle, and the toggle already works, but I just can’t change the color of the button. I have 2 materials set for the different states of the button, and I want the button to be Yellow when it is active, and White when it is idle. Here’s what I’ve tried:

mybutton.renderer.material = myMaterial; //compiles, does nothing (yes myMaterial is set)

mybutton.renderer.material.color = Color.yellow; //compiles, does nothing

mybutton.image.color = Color.yellow; //does not compile

mybutton.image.color.normalColor = Color.yellow; //does not compile

mybutton.colors.color = Color.yellow; //does not compile

All I want to do is change the color of the default state of the button once. If anyone can tell me how to do that, I’d be grateful. Thanks in advance.

Try choose “Color Tint” in Button Component, when u can set up normal, highlighted, pressed and disabled color. Its very simply, but this works only Unity 4.6, and 5. its new UI system in Unity.
Hope that my answer is helpfull

You can also edit the individual sections of the ColorBlock, this example script just switches the highlighted button between red and blue. It should give you an idea how it works:

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

public class ChangeColor : MonoBehaviour {

	public Button myButton;
	private bool redBlue;
	
	public void AlterColor()
	{
		ColorBlock myCB = myButton.colors;

		if(redBlue)
			myCB.highlightedColor = Color.red;
		else
			myCB.highlightedColor = Color.blue;

		redBlue = !redBlue;
		myButton.colors = myCB;
	}
}