Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Aer93 · Jun 24, 2011 at 12:45 PM · javascriptobjectsselecting

Selecting and unselecting objects with raycast

I made this in order to select objects :

 var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
 
 var hit : RaycastHit;
     
 //Debug.DrawRay (ray.origin, ray.direction * 100, Color.yellow);
 
 if (Physics.Raycast (ray.origin, ray.direction, hit, 100)) 
 
 {
         
         if(hit.collider.tag == "Object")
         {
             print ("Here is an object!");
         }
 
 
             var object : GameObject = hit.collider.gameObject;
         
             var myAI1 : MyAI1 = object.GetComponent(MyAI1);
             
             //we change the color
             myAI1.ChangeColor();
             
    
 }

It works fine, and i know when the mouse goes over the object, but how could I do to know when the object is being unselected, when the mouse is leaving it?

I mean, I want the object to change color when the mouse is over it. With that code I can only change the color when the raycast enters the object, but not when it leaves. So the object changes, but doesnt return back to the original color.

thank you for all the help!

Comment
Add comment · Show 3
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 flaviusxvii · Jun 24, 2011 at 04:26 PM 0
Share

I fixed your code formatting. PLEASE use the '010101' button to format your code in the future.

avatar image Anxo · Jun 24, 2011 at 04:45 PM 0
Share

not sure I understand your question. Are you asking how to set a boolean to true of false if the raycast is hitting the object?

avatar image Aer93 · Jun 24, 2011 at 06:23 PM 0
Share

I want the object to change color when the mouse is going over it. With that code I can only change the color when the raycast enters the object, but not when it leaves.

2 Replies

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

Answer by aldonaletto · Jun 24, 2011 at 07:38 PM

I think you should use OnMouseEnter and OnMouseExit instead. If the objects have a collider, these functions can do the job. Read about them in:

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseEnter.html

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseExit.html

Comment
Add comment · Show 4 · 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 Aer93 · Jun 24, 2011 at 07:41 PM 0
Share

Ok, I will read it but I thought that raycasting was the best way to select objects in 3d space. What do you think I should do? Thnx

avatar image aldonaletto · Jun 24, 2011 at 07:53 PM 0
Share

These functions already keep track of where the mouse pointer is, and which objects are "under" the pointer, so you should not need to do the raycast yourself. I've not tried them, but according to the docs they seem exactly what you're looking for.

avatar image Aer93 · Jun 24, 2011 at 07:59 PM 0
Share

And then, when should I use the raycast?

avatar image aldonaletto · Jun 25, 2011 at 12:32 AM 0
Share

I think you don't need the raycast - at least to select the objects with the mouse. The docs contain examples on how to change the color of a object when the mouse is over it.

avatar image
2

Answer by Mischa · Jun 24, 2011 at 06:32 PM

You would have to cache the selected object somewhere. If you just want to change the color back it would be something like that: (untestet code)

 ////this has to be outside the update loop
 var cachedObj : GameObject
 ////
 
 var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
 var hit : RaycastHit;
 
 //Debug.DrawRay (ray.origin, ray.direction * 100, Color.yellow);
 
 if (Physics.Raycast (ray.origin, ray.direction, hit, 100)) 
 {
         if(hit.collider.tag == "Object")
         {
             var object : GameObject = hit.collider.gameObject;
             if(object != cachedObj) //new object selected
             {
                 cachedObj.changeColorBack(); //unselect object
                 var myAI1 : MyAI1 = object.GetComponent(MyAI1);
                 myAI1.ChangeColor();
                 cachedObj = object; //cache the new selected object
             }
         }
 }
Comment
Add comment · Show 3 · 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 Aer93 · Jun 24, 2011 at 07:38 PM 0
Share

It doesnt work

cachedObj.changeColorBack(); //unselect object

Error: changeColorBack() is not a member of unity

I tried to fix it like this

var myAI2 : $$anonymous$$yAI1 = cachedObj.GetComponent($$anonymous$$yAI1); myAI2.changeColorBack();

but now I get another error:

cachedObj = $$anonymous$$yAI1

Error: Cannot convert 'System.Type' to 'UnityEngine.GameObject'

help

avatar image Mischa · Jun 24, 2011 at 08:31 PM 0
Share

changeColorBack() would be a custom function that you add to the $$anonymous$$yAI1 script. Same as changeColor(), but now you should reset it to the old value.

replace cachedObj = $$anonymous$$yAI1 with cachedObj = object. That was my hasty typing, sorry :-)

I edited the code above.

avatar image Aer93 · Jun 25, 2011 at 06:26 AM 0
Share

I added the ChangeColorBack() function in $$anonymous$$yAI1, but still the same problem

Error: 'ChangeColorBack' is not a member of 'UnityEngine.GameObject'.

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

6 People are following this question.

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

Related Questions

Selecting and unselecting objects with raycast 0 Answers

how to put the objects together (javascript) 1 Answer

Hiding and showing multiple objects 1 Answer

Carrying Objects OnMouseOver 0 Answers

Play sound when clicking object 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