hi,
I have five cubes in the scene and the player
with this code I can find the nearest transform ( cube game object )
//cubes script
//cubeScript.c#
using UnityEngine;
using System.Collections;
public class cubeScript : MonoBehaviour {
public bool closest;
void Start () {
}
void Update () {
}
}
//player script
//findClosestCube.c#
using UnityEngine;
using System.Collections;
public class findClosestCube : MonoBehaviour {
public GameObject[] cubes;
public Transform nearCube;
void Start () {
cubes=GameObject.FindGameObjectsWithTag("cube");
if(cubes.Length>0){
nearCube = cubes[0].transform;
}
}
void Update () {
for(int i=0;i<cubes.Length;i++){
if(Vector3.Distance(transform.position,cubes[i].transform.position) < Vector3.Distance(transform.position,nearCube.position)){
nearCube = cubes[i].transform;
nearCube.gameObject.GetComponent<cubeScript>().closest=true;
}
}
}
}
I also need to mark this cube as closest ( the closest property in cubeScript )
I need to update/change the closest property according to the nearest cube
the problem is: using nearCube.gameObject.GetComponent().closest=true;
more than one cube ( the nearCube) is marked as closest
hi,thanks
yes it works now,
but I still need to change the closest property to false ( of the cubes that are no longer the closest and still marked as true )