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 PsychoticStudios · May 17, 2013 at 04:05 PM · errorgameobjectdestroyraycasthit

Object reference not set to an instance of an object (with a RaycastHit)

I'm trying to destroy an object when I right click, and the game isn't paused, and the object my raycast collides with has the tag of "item". When I do this I get the error in the title about hit.transform.gameObject.tag == "item". Here's all of the code,

 var cube : GameObject;
 
 var cubeClone : GameObject;
 
 var moveFromPoint : float = 2;
 
 var atoms : float = 1000;
 
 @HideInInspector var paused : boolean;
 
 @HideInInspector var tooClose : boolean;
 
 function Update () {
     
     paused = Camera.main.GetComponent(FPSCamera).paused;
     
     //test to see if you are too close to a wall
     var hit : RaycastHit;
     if(Physics.Raycast(transform.position, transform.forward, 2))
             tooClose = true;
                 else
                     tooClose = false;
     
     //test to see if you can place cube    
     if(Input.GetButtonDown("Fire1") && tooClose == false && atoms >= 100 && paused == false){
         cubeClone =    Instantiate(cube, transform.position + transform.forward * moveFromPoint, Quaternion.Euler(0,0,0));
                 atoms = atoms - 100;
     }
     //test to see if you can remove
     if(Input.GetButtonDown("Fire2") && paused == false && hit.transform.gameObject.tag == "item"){
     Debug.Log("it works!");
     }
 }


the last if statement provides this error when I right click the "item", Object reference not set to an instance of an object. Which is referencing the hit.transform.gameObject.tag but I'm not sure why.

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 robertbu · May 17, 2013 at 04:21 PM

You need to pass the 'hit' to the Raycast() for it to be initialized, but even if you do that, at the point you want to use the 'hit' you don't know if it is initialized or not. You need to restructure your code. Here is a bit of a rewrite (untested):

 var cube : GameObject;
 var cubeClone : GameObject;
 var moveFromPoint : float = 2;
 var atoms : float = 1000; 
  
 function Update () {
  
     if (Camera.main.GetComponent(FPSCamera).paused)  // Just bail out if the game is paused
         return;
 
     //test to see if you can place cube    
     if(Input.GetButtonDown("Fire1") && atoms >= 100 ){
            if(!Physics.Raycast(transform.position, transform.forward, 2)) {  // Note the '!'
                cubeClone =   Instantiate(cube, transform.position + transform.forward * moveFromPoint, Quaternion.identity);
               atoms = atoms - 100;
          }
     }
     //test to see if you can remove
     if(Input.GetButtonDown("Fire2")){
         var hit : RaycastHit;
         if(Physics.Raycast(transform.position, transform.forward, hit, 2)) {
             if (hit.transform.gameObject.tag == "item") {
                 Debug.Log("it works!");
             }
         
         }
     }
 }
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 PsychoticStudios · May 17, 2013 at 04:52 PM 0
Share

Thanks for the help =)

avatar image
0

Answer by codecranker · May 17, 2013 at 04:10 PM

I dont think so 'hit' will automagically get assigned without being passed as an argument in Physics.Raycast() :D. Use another overloaded method for Physics.Raycast() that accepts RayCastHit as an argument.

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 Ches81 · May 17, 2013 at 04:25 PM

Hi, you need to pass the instance of RaycastHit "hit" to the method Physics.Raycast to get the hit informations you like to have. In your code you only check if something at the range of 2 get's hit and that's it. Your "hit" variable is still empty.

Here is the example of the Unity Script Reference:

   function Update () {
          var hit : RaycastHit;
 
         if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
 
             var distanceToGround = hit.distance;
         }
     }

Hope that helps.

Greetings. Christian

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

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

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Why won't my script load? 1 Answer

Why am i getting a null reference? 1 Answer

How would I destroy the gameobject that a raycast hit? (SOLVED) 1 Answer

Gameobject is destroyed even though I don't actively destroy it (or it's parent) 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