Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kerbo · Nov 12, 2012 at 08:54 PM · c#inputmouse

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;
         }
     }
     
     
 }

alt text

screen shot 2012-11-13 at 0.27.20.png (134.9 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sparkzbarca · Nov 13, 2012 at 02:07 AM 1
Share

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.

avatar image kerbo · Nov 13, 2012 at 10:21 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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");
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kerbo · Nov 13, 2012 at 11:20 AM 0
Share

Hello and big thanks for useful advice! - the problem is solved.

avatar image
0

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");
 }
 }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kerbo · Nov 13, 2012 at 10:43 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges