- Home /
Why am I getting the warning MoveWall.ColorA and MoveWall.ColorB are not assigned?
So, I'm just trying to make the GameObject switch between two colors using an input, but when I run the scene, the material color never changes from its default. I know this is a dumb question; I'm just starting out in C#. Thanks in advance!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MoveWall : MonoBehaviour {
GameObject Cube;
Color32 ColorA;
Color32 ColorB;
Color32 NewColor;
void Start () {
Cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
Cube.transform.position = new Vector3 (0, 0, 0);
NewColor = Cube.GetComponent<Renderer>().material.color;
ColorA = new Color32 (0, 0, 255, 0);
ColorB = new Color32 (255, 0, 0, 0);
}
void Update () {
if (Input.GetKeyDown(KeyCode.Q)){
ColorA = NewColor;
}
if (Input.GetKeyDown (KeyCode.W)) {
ColorB = NewColor;
}
}
}
Answer by Detour_Guide · Sep 28, 2017 at 04:08 AM
You're never changing the Cube's color.
On line 10 you're setting NewColor to the Cube's current color. So NewColor is now white. So, when you get to line 20, ColorA is blue and NewColor is white. Line 20 just sets ColorA to also be white.
To change the cube color you'll want to set the Cube's Material's color to one of your other colors instead of overwriting the actual color objects. Something like this should get you started.
if (Input.GetKeyDown(KeyCode.Q)){
Cube.GetComponent<Renderer>().material.color = ColorA;
}
Ahh...I see. So I had the variable/assignment backwards? I really get confused with that sometimes. Not sure why. Thanks for the response!
No worries. That's very common. Just remember the equal operator uses information from the right side to change things on the left side.
Perfect explanation! I'll have to make a diagram and stick it on my computer till it becomes more second nature.