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 Sephius · Jul 11, 2013 at 03:09 PM · gameobjectmouseclassmember

Mouse Action on a GameObject member of a class

Hello there !

I'm new to Unity, and i am trying to find a way to make my item react to a mouse action. I saw in the doc that it was possible to do that, but i can't figure out how it works...

Here is an example :

 public class Territory
 {        
             private GameObject GOTerritory;
     
         public bool MouseOverTerritory()
         {
             if(GOTerritory.collider. ??????????? ) // i think that i have to call something on the collider, but not sure
             {
                 GOTerritory.renderer.material = GameObject.Find("MFSelected").renderer.material;
                 return true;
             }
             else                
             {
                 GOTerritory.renderer.material = GameObject.Find("MFNotSelected").renderer.material;
                 return false;
             }
         }
 }


Here i try to change the material of my GameObject GOTerritory if my mouse is over it.

Am i looking in the good direction, or am i totally wrong about the way i should do it ?

PS : i don't assign this script to any gameobject. It is just a class that i use and i don't want it assignated to any gameobject. I just want to have my GameObjects as members of the class.

Thanks for your time, have a great day.

Comment
Add comment
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

3 Replies

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

Answer by markedagain · Jul 11, 2013 at 03:48 PM

does the GOTerritory have its own script ?

if so what u can do is in the GOTerritory script u can make a bool IsMouseOver and then use monobehaviour's OnMouseEnter and OnMouseExit to turn it off and on.

after that in your territory script u can do

 GOTerritory.transform.getComponent<GOTerritory>().isMouseOver
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 Sephius · Jul 11, 2013 at 04:18 PM 0
Share

Thank you for the answer ! I did it an other way, but i will keep your answer in $$anonymous$$d and will do some test. I'm not very familiar with theses script things that you assign to objects. It seems to be pretty usefull.

avatar image
0

Answer by Sephius · Jul 11, 2013 at 03:49 PM

Ok i figured out how it works. Just in case, i will put my code, if someone have the same question.

         public bool MouseOverTerritory()
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit = new RaycastHit();
             
             
             if(GOTerritory.collider.Raycast(ray,out hit,100))
             {
                 GOTerritory.renderer.material = GameObject.Find("MFSelected").renderer.material;
                 return true;
             }
             else
             {
                 GOTerritory.renderer.material = GameObject.Find("MFNotSelected").renderer.material;
                 return false;                
             }
         }
Comment
Add comment · 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
0

Answer by cdrandin · Jul 11, 2013 at 03:47 PM

Try playing with the example provided by Unity. Just make a new script and just run what they have and see the relationship. http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseOver.html

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 Sephius · Jul 11, 2013 at 04:19 PM 0
Share

Thank you for the answer, i will check this out :)

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

16 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 avatar image avatar image avatar image avatar image

Related Questions

Class hierarchy, gameObject, Raycast 1 Answer

Using a static class & pass in gameObject or just add same script to multiple objects 1 Answer

Unable to Call Instantiate in Class Member Function 0 Answers

Simple abstractions. Reusing code outside of class 1 Answer

move Game Object(hand) with mouse movement 2 Answers


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