- Home /
OnMouseEnter/Exit problem
Hi guys! I've already upgraded my code from here: http://answers.unity3d.com/questions/344408/procedural-array-of-meshes-problem.html
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;
}
}
}
what object is this script attached to, i'm betting its attached to that square sitting there in the corner.
Just to be clear this script isn't attached to all those new game objects.
and so this on mouse enter wont kick off when you mouse other portions of the plane.
To do that you need to create one portion of the plane. Attach the on mouse enter to that and then spawn instances of that object.
hi! the script attached to the empty GameObject, its transform is the parent for all another NewGameObjects, which are generated on the fly. The cube in the corner is just a measure tool i used for testing the script while generating an array of meshes.
Answer by Seth-Bergman · Nov 13, 2012 at 10:41 AM
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");
Hello and big thanks for useful advice! - the problem is solved.
Answer by Althaen · Nov 13, 2012 at 04:23 AM
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");
}
}
}
hi! i wrote for test a little piece of code as you suppose into if statements in Update(), for mouse down/released, unfortunately it still doesn't work. It seems to me that GetButtonUp/Down is not the best way..
Your answer
Follow this Question
Related Questions
C# Mouse Look (Input.GetAxis("MouseX&Y")) 0 Answers
Get GameObject That Was Last Clicked? 2 Answers
How to successfully apply force calling a function from another script 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Why is Input.GetAxisRaw() not returning whole numbers when using a joystick? 1 Answer