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 GamingGuru1992 · Aug 11, 2013 at 06:47 PM · raycastmousevariableclickray

Clicked object is stored in a variable?

I'm currently working on a piece of code that will allow me to move different objects depending on what I click on however I'm having some trouble finalizing it.

The idea is simple, I have a variable called target...

 var target: Transform;


...and I use this variable throughout the code. Currently however I have to preset this to an object from my scene (drag and drop it into the slot). What I need to be able to do is basically say

 target = object clicked on

I've looked into mouse ray casting but got the error "Cannot convert UnityEngine.GameObject to UnityEngine.Transform".

 if (Physics.Raycast (ray, hit, 100))
         {
             if(hit.collider.gameObject.tag == "Cube")
             {
                 target = hit.transform.gameObject;
             }
         }

Any help would be greatly appreciated.

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

Answer by robertbu · Aug 11, 2013 at 06:53 PM

Your 'target' is a Transform, so line 5 should be:

 target = hit.transform;
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 GamingGuru1992 · Aug 11, 2013 at 07:00 PM 0
Share

Tried that and get the error "Object reference not set to an instance of an object". The error says its line 25 which is the if (Physics.Raycast (ray, hit, 100)).

Just some more info as well, I'm having this script tied to the camera, I want it to be able to work for any object in the game, rather than placing the same script on hundreds of cubes over and over.

avatar image robertbu · Aug 11, 2013 at 07:07 PM 1
Share

I'd have to see the error in context to see what is going on. $$anonymous$$ost likely you are not handling a null Transform. That is, the target only get's assigned to hit.transform if the Raycast() hits something. If it does not, target is not changed. So unless you are giving it some initial value in the Inspector, it will be null. And trying to access a null reference generates the error.

So you probably need to do something like this around the code that uses target:

 if (target != null) {
 
 
 }
avatar image GamingGuru1992 · Aug 11, 2013 at 07:15 PM 0
Share

I don't have anything to handle a null transform yet but I don't see how that can be an issue right now. There should never be a null because it only triggers when the mouse is down and I only click the mouse over the object so it should always hit something?

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

15 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

Related Questions

click on a game object that script isn't attached to 1 Answer

Mouse Click and Spawn object 4 Answers

Checking if object stopped being hit by Raycast while overlapping another object of the same type. 0 Answers

Detect if object clicked on 2 Answers

C# mouse Raycast question 3 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