Question by
carloskassis · Jun 27, 2016 at 08:26 PM ·
gameobjectcolornullexceptionrgba
Make a relation between color and distance between objects' x coordinates
I'm trying to make the object to change color, it becomes whiter the closer it gets to the object "cubetarget", and i want to substract the x coordinates of the two objects and divide by 3, because the maximum distance between them is limited to 3, so maximum it'll be black, but I'm getting "Field "test.cubetarget" i never assigned to, and will always have its default value null". How can i fix it? and is there a better way doing this relation?
*Script's name is test.cs.
using UnityEngine; using System.Collections;
public class test : MonoBehaviour {
GameObject cubetarget;
public Renderer rend;
float r;
float g;
float b;
void Start () {
GameObject cubetarget = GameObject.Find("cubetarget");
rend = GetComponent<Renderer>();
}
void Update () {
r= (Mathf.Abs((gameObject.transform.position.x - cubetarget.transform.position.x)/3));
g= (Mathf.Abs((gameObject.transform.position.x - cubetarget.transform.position.x)/3));
b= (Mathf.Abs((gameObject.transform.position.x - cubetarget.transform.position.x)/3));
rend.material.color = new Color(r,g,b,1);
}
}
Comment