I am working through Junior Programmer: Create with Code 1, Mod the Cube.
I have coded on input to change the material color of the Cube. I have debugged the code and the material color is changing but does not show up in the game window or in the Unity Preview window. I have also sent my code to my professor who said it worked for him. Perhaps the fault is because I used editor version 6000.0.23f1 to create the project? Does anyone have any ideas why the color is not rendering? The other changes to rotation and scaling work fine.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Cube : MonoBehaviour
{
public MeshRenderer Renderer;
private float rotateX;
private float rotateZ;
private float redcolor;
private float bluecolor;
private float greencolor;
private float seethrew;
private Material material;
private Color newcolor;
private float cubescale;
private Color testcolor;
void Start()
{
cubescale = 1.3f;
transform.position = new Vector3(3, 4, 1);
transform.localScale = Vector3.one * cubescale;
/* redcolor = 0.5f;
bluecolor = 0.3f;
greencolor = 1.0f;
seethrew = 0.4f;*/
redcolor = 0.0f;
bluecolor = 0.0f;
greencolor = 0.0f;
seethrew = 1.0f;
material = Renderer.material;
newcolor = new Color(redcolor, greencolor, bluecolor, seethrew);
material.color = newcolor;
rotateX = 10.0f;
rotateZ = 0.0f;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
rotateX += 5f;
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
rotateX -= 5f;
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
rotateZ += 5f;
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
rotateZ -= 5f;
}
else if (Input.GetKeyDown(KeyCode.Alpha1) && redcolor < 1.0f)
{
redcolor = Mathf.Min(1.0f, (redcolor + 0.1f));
Debug.Log("red=" + redcolor.ToString("#.##"));
}
else if (Input.GetKeyDown(KeyCode.Alpha2) && redcolor > 0.0f)
{
redcolor = Mathf.Max(0.0f, (redcolor - 0.1f));
Debug.Log("red=" + redcolor.ToString("#.##"));
}
else if (Input.GetKeyDown(KeyCode.Alpha3) && bluecolor < 1.0f)
{
bluecolor = Mathf.Min(1.0f, (bluecolor + 0.1f));
Debug.Log("blue=" + bluecolor.ToString("#.##"));
}
else if (Input.GetKeyDown(KeyCode.Alpha4) && bluecolor > 0.0f)
{
bluecolor = Mathf.Max(0.0f, (bluecolor - 0.1f));
Debug.Log("blue=" + bluecolor.ToString("#.##"));
}
else if (Input.GetKeyDown(KeyCode.Z) && cubescale < 3.0f)
{
cubescale = Mathf.Min(3.0f, (cubescale + 0.2f));
}
else if (Input.GetKeyDown(KeyCode.X) && cubescale > 0.2f)
{
cubescale = Mathf.Max(0.2f, (cubescale - 0.1f));
}
//newcolor = new Color(redcolor, greencolor, bluecolor, seethrew);
newcolor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
//material = Renderer.material;
//material.SetColor("_Color", newcolor);
material.color = newcolor;
testcolor = material.color;
string rd = testcolor.r.ToString("#.##");
string bl = testcolor.b.ToString("#.##");
string gr = testcolor.g.ToString("#.##");
string al = testcolor.a.ToString("#.##");
Debug.Log(rd + " " + bl + " " + gr);
transform.Rotate(rotateX * Time.deltaTime, 0.0f, rotateZ * Time.deltaTime);
transform.localScale = Vector3.one * cubescale;
}
}