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 smnerat · Nov 22, 2012 at 02:27 AM · raycastcollidertransformhit

Assigning a transform using the ray cast collider.

Hey guys,

I want the GUI buttons I make to rotate the parent of the object I select, and I am having trouble getting the transform of the object I click on using raycast. I have attached my code in the hopes someone has an idea of what I am doing wrong.

The error I keep getting is : (NullReferenceException: Object reference not set to an instance of an object Select2.OnGUI () (at Assets/Select2.js:24)

 //var capsule : Transform;
 var clicked : boolean = false;
 var hit : RaycastHit;
 
 function Update() {
 
     var capsule : Transform;
     
     if(Input.GetMouseButtonDown(0) &&
        collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit,
                         Mathf.Infinity)) {
      
          capsule = hit.collider.GetComponent(Transform);
          
     }
 }
  
   function OnGUI()
         {
         
         var capsule = hit.collider.GetComponent(Transform);
         var someText = "Turn Left";
         var otherText = "Turn Right";
               
             if (GUI.Button(new Rect(20, 50, 100, 20), someText)){
                 //capsule = hit.collider.GetComponent(Transform);
                 capsule.parent.transform.Rotate (0, 0, 45, Space.World);
                 }
                
             else if (GUI.Button(new Rect(850, 50, 100, 20), otherText)){
                 //capsule = hit.collider.GetComponent(Transform);
                 capsule.parent.transform.Rotate (0, 0, -45, Space.World);
                 }
   
         }
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 YM_Industries · Nov 22, 2012 at 05:49 AM 0
Share

Have you modified the spacing when you pasted this? Because Line 24 is a blank line.

avatar image smnerat · Nov 23, 2012 at 12:58 AM 0
Share

Hmm, I'm not sure what happened, the line in question is right after function OnGUI()

"var capsule = hit.collider.GetComponent(Transform);".

2 Replies

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

Answer by Loius · Nov 23, 2012 at 09:21 PM

Alright, back up a bit.

In all programming, whenever (every time) you use any object which can be null, you must FIRST be sure that the object is not null. If you don't check, you'll have errors - period.

The simplest and least error-prone way to do this is:

 if ( object ) {
   if ( object.child ) {
     if ( object.child.member ) {
       object.child.member.DoSomethin();
     }
     object.child.DoSomething();
   }
   object.DoSomething();
 }

Often, you can be assured that the object is not null due to code flow - for instance:

 function OnTriggerEnter( other : Collider ) {
   // other cannot possibly be null
   other.SendMessage("HitBy" + name);
 }

Or:

 function Setup() {
   object = new Thingy();
   object.DoSomething();
 }

So for your specific problem, you have a couple of things which can be null - capsule, hit.collider, someText, and otherText. You know someText and otherText aren't null - you just created them. You also know that you only have a capsule when hit.collider is not null. And there's no reason to show the buttons if you don't have a capsule.

So: you can put most of the OnGUI function into that check for hit.collider and then be guaranteed that capsule is not null, or you can check for null capsule before trying to turn it.

Comment
Add comment · Show 2 · 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 smnerat · Nov 23, 2012 at 10:51 PM 0
Share

I've been trying this, but the only variations of my code that even compile will give me errors. The main one I get is that "You can only call GUI functions from inside OnGUI".

avatar image Loius · Nov 24, 2012 at 02:37 AM 0
Share

Well, you can only call GUI functions from inside OnGUI. The function. Which you have already.

avatar image
0

Answer by Loius · Nov 23, 2012 at 01:25 AM

Hit has not been set until you set it.

You need to check it first:

if ( hit ) { /do all your stuff here/ }

Comment
Add comment · Show 2 · 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 smnerat · Nov 23, 2012 at 03:54 AM 0
Share

I was looking through some posts and ended up trying that before. I get an error that "UnityEngine.RaycastHit cannot be used in a boolean context".

avatar image Loius · Nov 23, 2012 at 07:07 PM 0
Share

if (hit.collider)

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

Click&Drag Misterious Disappearing! 1 Answer

I don't know why i can't detect by ray sth. tagged, 1 Answer

Lag when I change the layer of a game object ingame 0 Answers

How to hit two object with one raycast? 2 Answers

Raycasting and receiving hit.colliders with two cameras 0 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