HELP GetComponent

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

Maybe something like this?

private void Update() 
{
    GameObject nearestCube = null;
    float nearestDistance = float.MaxValue;

    // Start by finding the nearest, don't do anything else.
    for (int i = 0; i < cubes.Length; i++) 
    {
        float distance = Vector3.Distance(transform.position, cubes[i].transform.position);
        if (distance  < nearestDistance)
        {
            nearestCube = cubes[i];
            nearestDistance = distance;
        }
    }

    // Nearest found! null if no cubes exist.
    if (nearestCube != null) 
    {
        nearestCube.GetComponent<cubeScript>().closest = true;
        nearCube = nearestCube.transform;
    }
}

I believe this should work and will be more efficient as it avoids calls to GetComponent in the main loop:

using System.Linq;
using UnityEngine;

public class FindClosestCubeScript : MonoBehaviour
{
    public CubeScript[] Cubes;
    public CubeScript NearestCube;
       
    private void Start()
    {
        Cubes = GameObject.FindGameObjectsWithTag("cube").Select(c => c.GetComponent<CubeScript>()).Where(c => c != null).ToArray();
        FindClosestCube();
    }
       
    private void Update()
    {
        FindClosestCube();
    }

    private void FindClosestCube()
    {
        float? nearestDistanceSq = null;
        foreach(var cube in Cubes)
        {
            cube.Closest = false;
            var distanceSq = (cube.transform.position - transform.position).sqrMagnitude;
            if(nearestDistanceSq == null || distanceSq < nearestDistanceSq.Value)
            {
                if(NearestCube != null)
                {
                    NearestCube.Closest = false;
                }

                cube.Closest = true;
                NearestCube = cube;
                nearestDistanceSq = distanceSq;
            }
        }
    }
}

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 )

if (nearCube != null)
    nearCube.gameObject.GetComponent<cubeScript>().closest = false;

You could add that at the start of the Update method. So after an update, only one cube would be “closest”.

thanks!

hi,Roland1234
I’ll try this later,Thanks!