If statements with a greater/less than than symbol not working but greater/less than or equal to symbol is

I realize this problem is basic, and I just want to say that I’ve looked around for the solution, and even looked on this site to see if I could find the answer, but couldn’t. So I’m now wondering if it’s a bug causing this.

In my if statements within my IEnumerator, if I have a greater/less than symbol (> or <) my if statement has been skipped, but if I have a greater/less than or equal to symbol (>= or <=), it works fine. I added Debug.Log statements to each if statement within the IEnumerator to see whether or not they are running.

Here is my code:

using UnityEngine;
            using System.Collections;
            
            public class ChangeColor : MonoBehaviour {
            
            
            
            	public Renderer enemy1DGreen;
            	private Renderer rend;
            
            	void Awake(){
            		
            			rend = GetComponent<Renderer> ();
            
            	}
            
            
            	void Start () {
            		
            			StartCoroutine (changeColor ());
            	
            	}
            		
            	IEnumerator changeColor(){
            
            
            		GameObject player = GameObject.Find("Player");
            		while (player != null) {
            
            			
            			if (transform.localScale.x > player.transform.localScale.x) {
            				Color color = Color.yellow;
            
            
            				rend.material.SetColor ("_Color", color);
            				Debug.Log ("Color Yellow");
            
            			} else if (transform.localScale.x <= player.transform.localScale.x) {
            				Color color = Color.green;
            
            
            				rend.material.SetColor ("_Color", color);
            
            				Debug.Log ("Color Green");
            			} else {
            
            				Debug.Log ("No color change");
            
            			}
            
            
            			yield return new WaitForSeconds (0.1f);
            
            		}
            
            
            	}
            
            }

This code will print “Color Green” to the console every time the while loop loops, no matter the size of the player’s transform, but if I were to take the “=” out of the “else if” statement (change “<= to <”) then both the “if” statement and the “else if” statement will be skipped and it will run the “else” statement and print “No color change” to the console every time it loops.

Well, that most likely means your two object are exactly of equal size. Where is this script attached to? Is it possible that you attached it to your player? Because in this case “player.transform” and “transform” reference the same object. So no matter what size your object has you always compare it with itself. So obviously both x-scales will be equal at any time.

Just to make that clear: There is no bug in the less / greater than operator. When you start debugging a problem your first assumption should be: “Did I something wrong?”. Have you already tried printing the actual scale of each of your objects? If they are equal, try printing the object names. When you add “Debug.Logs / prints” make sure you print meaningful and distinguishable text. For example just printing the scale of both is just a mess since you can’t tell which one is which.

Try something like this:

while (player != null) {
    Debug.Log("this object: " + gameObject.name + " scale:" + transform.localScale);
    Debug.Log("player object: " + player.name + " scale:" + player.transform.localScale);