Reputation:
I'm trying to change the colours on a UI Button using this line of code.
prev.GetComponent<Button>().colors.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
but I'm receiving this error
Assets/_Scripts/OptionSwitch.cs(28,53): error CS1612: Cannot modify a value type return value of `UnityEngine.UI.Selectable.colors'. Consider storing the value in a temporary variable
I've tried storing both the button and the color as variables before calling them but it doesn't change the error code.
EDIT:
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Sprites;
public class OptionSwitch : MonoBehaviour {
ColorBlock colorBlock = new ColorBlock();
colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
[MenuItem ("GameObject/UI/Switch")]
static void Switch(){
if (GameObject.FindObjectOfType (typeof(Canvas)) != null) {
Canvas canvas = (Canvas)GameObject.FindObjectOfType (typeof(Canvas));
// Define Previous Button
GameObject prev = new GameObject ("Previous", typeof(Button));
prev.layer = 5;
prev.AddComponent<Image> ();
prev.transform.parent = canvas.transform;
prev.GetComponent<Image> ().sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd");
prev.GetComponent<Button>().colors = buttonColors;
// Define Previous Button Image
GameObject previm = new GameObject("Previous Image", typeof(RawImage));
previm.layer = 5;
previm.transform.parent = prev.transform;
} else {
// Create Canvas
GameObject canvas = new GameObject("Canvas", typeof(Canvas));
canvas.AddComponent<CanvasScaler> ();
canvas.AddComponent<GraphicRaycaster> ();
canvas.layer = 5;
canvas.GetComponent<Canvas> ().renderMode = RenderMode.ScreenSpaceOverlay;
canvas.transform.localPosition = Vector3.zero;
// Create Event System
GameObject eventsystem = new GameObject("EventSystem", typeof(EventSystem));
eventsystem.AddComponent<StandaloneInputModule>();
eventsystem.AddComponent<TouchInputModule>();
}
}
}
Upvotes: 3
Views: 8810
Reputation: 1
You can use button's image component and modify its color to change the background color of the button. To achieve this, you may use the following code:
btn.GetComponent<Image>().color = yourColor;
btn.GetComponent<Image>().color = new Color(0.0f, 0.0f, 0.0f, 1.0f);
Alternatively, you can utilize a ColorBlock
. By copying the existing color properties of the another button, you can adjust the normalColor
component before applying it back to the button:
ColorBlock colorBlock = btn1.GetComponent<Button>().colors;
colorBlock.normalColor = yourColor;
btn2.GetComponent<Button>().colors = colorBlock;
Upvotes: -1
Reputation: 1
ColorBlock colorBlock = new ColorBlock();
colorBlock.normalColor = Color.yellow;
colorBlock.colorMultiplier = 1;
gameManager.setCurrentLevelBoardRef.currentButtonArray[numbersToColor[i]]
.GetComponent<Button>().colors= colorBlock;
Upvotes: 0
Reputation: 1
I don't know what's up with all the .GetComponents
but there is another way too !
This method works for when you have multiple buttons and want to just change their colours or make them disappear.
Color.red , Color.grey , ...
for (int i = 0; i < 5; i++) {
alchemistVoScript.gatedButtons [i].image.color = Color.clear;
alchemistVoScript.gatedButtons [i].interactable = false;
}
Upvotes: -3
Reputation: 125305
You have to change the colors
not the normalColor
. The GetComponent<Button>().colors
returns ColorBlock
.
So, create a new instance of ColorBlock
. Modify normalColor
from that ColorBlock
then assign that ColorBlock
to the GetComponent<Button>().colors
.
Full example:
ColorBlock colorBlock = new ColorBlock();
colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
prev.GetComponent<Button>().colors = colorBlock;
This will overwrite your other color settings. To preserver them, create your ColorBlock
from prev.GetComponent<Button>().colors;
ColorBlock colorBlock = prev.GetComponent<Button>().colors;
colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
prev.GetComponent<Button>().colors = colorBlock;
You can also modify the color properties below:
colorBlock.pressedColor = new Color(1f, 0.0f, 0.0f, 1.0f);
colorBlock.highlightedColor = new Color(0f, 1f, 0.0f, 1.0f);
colorBlock.disabledColor = new Color(0f, 0f, 1, 1.0f);
Upvotes: 5