OnMouseEnter/Exit problem

Hi guys! I’ve already upgraded my code from here:

It all works perfectly except one thing: OnMouseXXX methods don’t work (I also tried using Input.mousePosition, but no reaction detected). Maybe the problem is in smth related to raycasting? I’m not totally sure.

Here is the code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class test_cuba : MonoBehaviour {
	
	List <Mesh> shtuchki = new List<Mesh>();
	List <GameObject> myshi_obj = new List<GameObject>();
	List <Rect> baseCoords = new List<Rect>();

	void Start () {	
		
				
		for(int n= 0; n < 10; n++){
			for (int m=0; m<10; m++){
				
				MeshFilter mfilter = gameObject.GetComponent<MeshFilter>();
				Mesh messh = new Mesh();
				mfilter.sharedMesh = messh;
				
				Rect rt = new Rect (n,m,1,1);
				baseCoords.Add(rt);
				
				/*Debug.Log ( rt.x);
				Debug.Log (rt.y);*/

				messh.vertices = new Vector3 [] {
		
					new Vector3(rt.x,rt.y,0), // ver0
					new Vector3(rt.xMax,rt.y,0), // ver1
					new Vector3(rt.xMax,rt.yMax,0), 	// ver2		
					new Vector3(rt.x,rt.yMax,0), //ver3
			
				};
		
				messh.triangles = new int [] {
					0,1,2,
					2,3,0,
			
				};
				//renderer.material.color = Color.white;
				
				messh.RecalculateBounds();
				messh.RecalculateNormals();
				shtuchki.Add(messh);	
				
				//Debug.Log("mesh_number: " + shtuchki.Count); 
				
			}
		}	
		
		//Graphics.DrawMesh(messh, transform.localToWorldMatrix, mat, 0);	
		
		for(int a = 0; a<shtuchki.Count; a++){
			GameObject tmp = new GameObject();
			myshi_obj.Add(tmp);
			MeshFilter meshf = myshi_obj[a].AddComponent(typeof(MeshFilter)) as MeshFilter;
		    MeshCollider meshc = myshi_obj[a].AddComponent(typeof(MeshCollider)) as MeshCollider;
			MeshRenderer meshr = myshi_obj[a].AddComponent(typeof(MeshRenderer)) as MeshRenderer;

			meshf.sharedMesh = shtuchki[a];
			meshc.sharedMesh = shtuchki[a]; 

			myshi_obj[a].transform.parent = gameObject.transform;				
			myshi_obj[a].renderer.material.color = Color.white;
			
		}
	}	
	
	void Update () {
		
		Debug.Log("mesh_total: " + shtuchki.Count); //meshes
	    Debug.Log("myshi_obj_total: " + myshi_obj.Count); //gameObjects
		Debug.Log("rects_total: " + baseCoords.Count); //rectangle-based_coords

		for(int v=0; v<shtuchki.Count; v++){
			Graphics.DrawMeshNow(shtuchki[v], Vector3.zero, Quaternion.identity, 0);
			/*if(baseCoords[v].Contains(Input.mousePosition)){
				myshi_obj[v].renderer.material.color = Color.red;
				Debug.Log("inside");
			}*/
		}	
		
	}
	
	void OnMouseEnter(){
		for(int p=0; p<myshi_obj.Count; p++){
			myshi_obj[p].renderer.material.color = Color.red;
			Debug.Log("inside");

			
		}
	}
	
	void OnMouseExit(){
		for(int v=0; v<myshi_obj.Count; v++){
			myshi_obj[v].renderer.material.color = Color.white;
		}
	}
	
	
}

[4900-screen+shot+2012-11-13+at+0.27.20.png|4900]

your OnMouse function would need to be on the SAME OBJECT as the COLLIDER, in other words, you can create a script which contains this:

void OnMouseEnter(){
     renderer.material.color = Color.red;
   }
void OnMouseExit(){
     renderer.material.color = Color.white;
   }

and add a copy to each individual child as you create them:

...etc
 MeshCollider meshc = myshi_obj[a].AddComponent(typeof(MeshCollider)) as MeshCollider;
 MeshRenderer meshr = myshi_obj[a].AddComponent(typeof(MeshRenderer)) as MeshRenderer;
 myshi_obj[a].AddComponent("MyMouseScriptName");

I think you’re not checking for the mouse button to be pressed so it never works. I put your onmouseenter functions into if statements in Update. I think that will work.

void Update () {

   Debug.Log("mesh_total: " + shtuchki.Count); //meshes
    Debug.Log("myshi_obj_total: " + myshi_obj.Count); //gameObjects
   Debug.Log("rects_total: " + baseCoords.Count); //rectangle-based_coords

   for(int v=0; v<shtuchki.Count; v++){
     Graphics.DrawMeshNow(shtuchki[v], Vector3.zero, Quaternion.identity, 0);
     /*if(baseCoords[v].Contains(Input.mousePosition)){
      myshi_obj[v].renderer.material.color = Color.red;
      Debug.Log("inside");
     }*/

if(Input.GetButtonDown("Fire1"){
   for(int v=0; v<myshi_obj.Count; v++){
     myshi_obj[v].renderer.material.color = Color.white;
   }
}

if(Input.GetButtonUp("Fire1"){
   for(int p=0; p<myshi_obj.Count; p++){
     myshi_obj[p].renderer.material.color = Color.red;
     Debug.Log("inside");
}
}
}