On MouseOver not working

Hello there,

I spent past four hours trying to make this script work:

using UnityEngine;
using System.Collections;

public class subSceneSelected : MonoBehaviour {


	// Use this for initialization
	public float period = 2;
	public Color color = Color.red; 
	private Color originalColor; 

	private bool subsceneSelected = false;
	


	/* Blinking started */
                void Start () {
	  subsceneSelected = false;
                  originalColor = gameObject.renderer.material.color;
                  StartCoroutine(CheckEnable());
                  }
	 
                public IEnumerator CheckEnable() {
                while (true){ 
                 if (!subsceneSelected) {//if subscene is not selected, change color 
	   yield return StartCoroutine(ChangeColor());        
                 }
                 yield return 0;
                }
                }
	
               public IEnumerator ChangeColor() {
                  Material[] stuff= gameObject.renderer.materials;
	
	   for (int i=0; i < gameObject.renderer.materials.Length; i++){
	   
	  if (stuff[i].color == originalColor) {
  	   stuff[i].color = color;


	   }else {
	   stuff[i].color = originalColor;

	   }  
	   }
 	  yield return new WaitForSeconds(period);
                }
	
                void Update(){
	   
	   
	}

	void onMouseDown () {
		
	if (subsceneSelected == false)
	 subsceneSelected = true;
                    
              //It then does something
             }
}

When attached to an object, object blinks, and when this object is being selected with the mouse,on Mouse Down function, it then stops blinking and it then does something. But it’s never called. I’ve attached this script to some objects that have another scripts with MouseDown functions and they are working fine, but this isn’t. I even tried to comment code with blinking and it0s not working…or I’m just getting tired of Unity. Nothing makes sense…Do you have suggestion?

OK, first off… You were calling a coroutine from a coroutine. I don’t know if this is a good thing, so I changed it up to calling a subroutine instead.

Further, I changed the way your CheckEnable function works in relation to the material color you are dealing with. I added a piece that specifies which color to make the object. (either the new color or the old color)

In the coroutine, I seperated the color transfer into a defined timeline, utilizing yields and such to turn them. I also specified which points need the new color and which the old.

Lastly, I changed the onMouseDown section to toggle the subsceneSelected variable. This means you click to turn it on, and you click to turn it off.

using UnityEngine;
using System.Collections;

public class subSceneSelected : MonoBehaviour {
	// Use this for initialization
	public float period = 2;
	public Color color = Color.red; 
	private Color originalColor; 

	private bool subsceneSelected = false;



	/* Blinking started */
	void Start () {
		subsceneSelected = false;
		originalColor = gameObject.renderer.material.color;
		StartCoroutine(CheckEnable());
	}
	 
	public IEnumerator CheckEnable() {
		while (true){
			if (!subsceneSelected) {//if subscene is not selected, change color 
				yield return new WaitForSeconds(period);
				ChangeColor(false);
				yield return new WaitForSeconds(period);
				ChangeColor(true);
			} else {
				ChangeColor(true);
			}
			yield return 0;
		}
	}

	public void ChangeColor(bool makeOriginal) {
		Material[] stuff= gameObject.renderer.materials;
		for (int i=0; i < gameObject.renderer.materials.Length; i++){
			if (!makeOriginal) {
				stuff[i].color = color;
			}else {
				stuff[i].color = originalColor;
			}  
		}
	}

	void onMouseDown () {
		subsceneSelected=!subsceneSelected;
	}
}

Thanks BigMisterB! This now looks pretty nice, I’ll use it…I made mistake when submitting this post so there are now two same threads in the forum. The problem was, onMouseDown function. It should be OnMouseDown.

BigMisterB is the man!