Home > Blockchain >  Switch color for all objects in an array
Switch color for all objects in an array

Time:07-12

I am trying to replicate the mobile game stickman hook in unity I want to change the color of a circle to purple when it's the closest, and all others to white. For some reason, this code works for only one circle, but not any more. I have been trying for some time to figure it out, but I can't. Here the code

 void FindClosestPoint()
    {
        float DistanceToClosestPoint = Mathf.Infinity;
        
        GameObject[] points = GameObject.FindGameObjectsWithTag("Swing");
        foreach (GameObject point in points)
        {
            float DistanceToPoint = (point.transform.position - tf.position).sqrMagnitude;
            

            
            if(DistanceToPoint < DistanceToClosestPoint)
            {
                DistanceToClosestPoint = DistanceToPoint;
                closestPoint = point;
                closestPointSprite = closestPoint.GetComponent<SpriteRenderer>();
                closestPointSprite.color = Color.magenta;
            }
            
            
           
            if(DistanceToPoint != DistanceToClosestPoint )
            {
                closestPointSprite.GetComponent<SpriteRenderer>().color = Color.white;
            }
            
           
        } 

CodePudding user response:

Your code iterates through all objects and does two things: if they are closer to the previous object, they color it purple, and if the distances are different, they color it white. With this system, if a there are 3 objects: A, B and C, where A is at (0, 0), B (5, 0) and C at (-2, 0), it will be colored purple before A because it is the first analyzed and therefore its distance is less than infinity, then B for white and C for purple because it is the closest. Also your code can be greatly improved for performance, for example by not using foreach and GameObject.FindGameObjectsWithTag. In the example I will use for, but I will have to keep using GameObject.FindGameObjectsWithTag like you did, but I recommend that you find an alternative to iterate objects without this function.

void FindClosestPoint()
{
    float DistanceToClosestPoint = Mathf.Infinity;    
    GameObject[] points = GameObject.FindGameObjectsWithTag("Swing”);
    for (int i = 0; i < points.Lenght; i  )
        points[i].GetComponent<SpriteRenderer>().color = Color.white;

     for (int i = 0; i < points.Lenght; i  )
     {
         float DistanceToPoint = (point[i].transform.position - tf.position).sqrMagnitude;
            
          if(DistanceToPoint < DistanceToClosestPoint)
          {
              DistanceToClosestPoint = DistanceToPoint;
              closestPoint = points[i];
          }
      }
      closestPointSprite = closestPoint.GetComponent<SpriteRenderer>();
      closestPointSprite.color = Color.magenta; 
}

In my example I first make all objects white, find the closest one and paint it purple at the end.

I hope I have helped you. You can mark my answer as accepted to thank me :)

CodePudding user response:

if(DistanceToPoint != DistanceToClosestPoint )
{
    point.GetComponent<SpriteRenderer>().color = Color.white;
}
  • Related