Hello Guys,
Another question as I progress with my learning coding in Unity. I have added:
- A plane
- Character Controller (Named ‘Matt’ in Hierarchy)
- A Cube (Which should change color to Red on keypress and also if Character Controller collides with the collider on cube) + The script is attached to the cube
I would like the CUBE to change it’s color if R key is pressed (which works) or if the player controller collides with the cube’s collider.
using UnityEngine;
using System.Collections;
public class colorChange : MonoBehaviour {
public GameObject cube;
private Renderer rend;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// This will get the OBJECT to CHANGE COLOR on KEY PRESS
if (Input.GetKeyDown (KeyCode.R))
GetComponent<Renderer> ().material.color = Color.red;
print ("A Key Pressed For Red Color");
}
void OnCollisionEnter (Collision col)
{
if (col.collider.name == "Matt")
{
rend.material.color = Color.yellow;
}
}
}
Thank you for your help ![]()
Waqas