I have this code to fade a object when it gets far from the camera. It is a fern object with a plane child object. The script is on the fern object. It works fine for one object, but if I copy the object, only the first one fades. How can I access the game object “plane” for each fern and fade them individually? Here is the code:
using UnityEngine;
using System.Collections;
public class DisapearDistance : MonoBehaviour
{
public float far_distance = 10.0f;
public float near_distance = 5.0f;
private Transform cam;
private GameObject plane_obj;
private float dist_to_cam;
private Color fade;
private float color_alpha;
void Start ()
{
cam = GameObject.FindGameObjectWithTag(Tags.camera).transform;
plane_obj = GameObject.Find("plane");
fade = plane_obj.renderer.material.color;
}
void Update()
{
dist_to_cam = Vector3.Distance(cam.transform.position, transform.position);
if(dist_to_cam > far_distance)
{
plane_obj.SetActive (false);
}
else if(dist_to_cam > near_distance)
{
plane_obj.SetActive (true);
color_alpha = 1 - ((dist_to_cam - near_distance) / (far_distance - near_distance));
fade = new Vector4(fade.r, fade.g, fade.b, color_alpha);
plane_obj.renderer.material.color = fade;
}
}
}
Thanks.