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
0
Question by matthrewp · Sep 24, 2016 at 02:59 PM · mouseclick objects

Select and save object

I need to be able to click an object in game, and assign a GameObject variable to it, like so:

 GameObject Selected;    
 Selected = (Object Clicked);

But none of the resources I go to seem to work. How do I do this?

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

1 Reply

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

Answer by Trevdevs · Sep 24, 2016 at 03:16 PM

Don't worry about assigning the camera it should work just fine :) might have forgotten something let me know though

     public Camera camera;
     public GameObject Selected;
 
     void Start()
     {
          camera = Camera.main;
     }
 
 
     void OnMouseOver()
     {
         if(Input.GetMouseButtonDown(0))
         {
             Ray ray = camera.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
             if(Physics.Raycast(ray, out hit))
             {
                 hit.collider.gameObject = Selected;
             }
         }
     }
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 matthrewp · Sep 24, 2016 at 03:33 PM 0
Share

It looks like it should work, but after inserting some prints in various locations, it appears On$$anonymous$$ouseOver isn't called, because "$$anonymous$$ouse Over" is never printed. Any idea why? Here's what I have (excluding the camera stuff):

     void On$$anonymous$$ouseOver()
     {
         print("$$anonymous$$ouse Over");
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             print("Input");
             Ray ray = Cam.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
             {
                 print("Object set");
                 ObjectSelected = hit.collider.gameObject;
             }
         }
     }
avatar image matthrewp · Sep 24, 2016 at 03:38 PM 0
Share

After changing On$$anonymous$$ouseOver() to Update(), It appears there is another error in the line:

 Ray ray = camera.ScreenPointToRay (Input.mousePosition); 

It complains that:

 NullReferenceException: Object reference not set to an instance of an object
 GameController.Update () (at Assets/Scripts/GameController.cs:29)
avatar image Trevdevs matthrewp · Sep 24, 2016 at 04:26 PM 0
Share

On$$anonymous$$ouseOver is called anytime the mouse is over a "GUIElement or a Collider" so the object your trying to select must be a 2d gui element or must include some form of a collider.

the reason for the error might be that camera is not being set to the main camera did you include the start function as well?

avatar image matthrewp Trevdevs · Sep 24, 2016 at 04:37 PM 0
Share

I set the camera manually and now it works fine. Thanks! Here is the finished code for any people who want to use it:

     public Camera Cam;
     GameObject ObjectSelected;
     void Update()
     {
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             Ray ray = Cam.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
             {
                 ObjectSelected = hit.collider.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

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

Related Questions

How to destroy the clone that I click on Unity 2D 1 Answer

How to click only one once? 0 Answers

How to detect a mouse click on a 2d gameobject? 1 Answer

how to simulate mouse click with fake cursor 0 Answers

I Need help with trees colliding 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