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.