- Home /
Getting errors with Color type
So I'm trying desperately to follow this tutorial on how to create a button puzzle: https://www.youtube.com/watch?v=h9ye2lU4lhw
However, I am coming across a problem. You see, in the tutorial, the guy is dealing with sprites for his buttons, whereas I just want to use solid colors.
My main problem arises due to this. You see, I still want to get the same results as the guy in the video, but I just want my buttons' colors to change instead of their sprites, but I don't know how to do it, and the attempts I have made so far have led to plentiful errors. So, I will show you the script the guy produces in the video and what I thought was the correct way to change it to my specifications:
His script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class GameController : MonoBehaviour {
public List<Button> btns = new List<Button>();
// Use this for initialization
void Start () {
GetButtons ();
AddListeners ();
}
// Update is called once per frame
void GetButtons () {
GameObject[] objects = GameObject.FindGameObjectsWithTag ("PuzzleButton");
for (int i = 0; i < objects.length; i++) {
btns.AddObjects [i].GetComponent<Button> ();
btns [i].image.sprite = bgImage;
}
}
void AddListeners(){
foreach (Button btn in btns) {
btn.onClick.AddListener(() => Puzzle());
}
}
public void Puzzle (){
string name = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name;
Debug.Log ("You are clicking on a button named: " + name);
}
}
My idea on what to change (I only changed variable assignments and the GetButtons function before I gave up :/)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class GameController : MonoBehaviour {
public List<Button> btns = new List<Button>();
public Color activeColor = Color.yellow;
public Color inactiveColor = Color.black;
// Use this for initialization
void Start () {
GetButtons ();
AddListeners ();
}
// Update is called once per frame
void GetButtons () {
GameObject[] objects = GameObject.FindGameObjectsWithTag ("PuzzleButton");
for (int i = 0; i < objects.length; i++) {
btns.AddObjects [i].GetComponent<Button> ();
btns [i].color = inactiveColor;
}
}
void AddListeners(){
foreach (Button btn in btns) {
btn.onClick.AddListener(() => Puzzle());
}
}
public void Puzzle (){
string name = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name;
Debug.Log ("Click named" + name);
}
}