hi every one
I have some gameobjects named them lightcolor… and their tag is “LightColorBlue” … I want to say if any game objects with this tag get near this gameobject , do somthing… when I put public GameObject lightcolor and I put a GameObject in unity inspector it work perfect , but I don’t know how to say "if any gameobjects that have that tag get near… " I used below code but doesn’t work!!
using UnityEngine;
using System.Collections;
public class NoColorTree : MonoBehaviour {
private GameObject lightcolor;
void Start()
{
lightcolor = GameObject.FindWithTag("LightColorBlue");
}
void Update()
{
if (Vector3.Distance(lightcolor.transform.position, transform.position) < 10)
{
renderer.material.color = Color.blue;
}
}
}
I changed your code slightly so that it is the objects with the tag ‘LightColorBlue’ that will change color (in your code it was the object containing the script FindTestTag.
Also I added another color when the object was not found (maybe you don’t need this - but it makes it easy to see in a test-case).
The new modified script is:
using UnityEngine;
using System.Collections;
public class FindTestTag : MonoBehaviour {
private GameObject[] lightcolors;
// Use this for initialization
void Start () {
lightcolors = GameObject.FindGameObjectsWithTag("LightColorBlue");
}
// Update is called once per frame
void Update () {
foreach (GameObject lightcolor in lightcolors){
if (Vector3.Distance(lightcolor.transform.position, transform.position) < 10)
{
lightcolor.renderer.material.color = Color.blue;
} else {
lightcolor.renderer.material.color = Color.red;
}
}
}
}