Quantcast
Channel: Questions in topic: "exception"
Viewing all articles
Browse latest Browse all 470

Make a relation between color and distance between objects' x coordinates

$
0
0
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(); } 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); } }

Viewing all articles
Browse latest Browse all 470

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>