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 /
This question was closed Sep 24, 2013 at 11:10 PM by clunk47 for the following reason:

OP decided to go with different approach, issue resolved.

avatar image
0
Question by ardizzle · Sep 23, 2013 at 09:54 PM · raycasthitdetectionridgidbody

Why is this giving me a null refrence exeption?

I have a ray that shoots out of this collider and when it hits its suppose to detect that it hit an AI but not the player. So I tried this code:

 if(hit.rigidbody.tag == "AI")

But it keeps giving me a null refrence exeption. But if I try it like this:

 if(hit.rigidbody == True)

It hits the AI and the player. Why is it not finding the tag? Is there a better way to go about this?

Edit:

This is the code thats on the collider

 #pragma strict
 var carWaiting   : boolean = false;
 private var stop : String  = "Intersection";
 private var go   : String  = "Go";
 function Start ()
 {
     // This makes sure the colliders tag starts out as stop
     gameObject.tag = stop;
 }
 
 function Update () 
 {
     var back = transform.TransformDirection (Vector3.back);
      var hit : RaycastHit;
         
     if(Physics.Raycast(transform.position, back, hit, 100))
     {
      // The ray detects it hits something here
         if(hit.collider.gameObject.tag == "AI")
         {
                    //but doesn't detect the AI here
             print("I see a computer car");
             carWaiting = true;
         }
     }
 }
Comment
Add comment · Show 1
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 clunk47 · Sep 23, 2013 at 10:10 PM 2
Share

Change

hit.rigidbody.tag

to

hit.collider.gameObject.tag

Also, you can just use if(hit.rigidbody) to check if is true, or if(!hit.rigidbody) to check if something is false, hence the '!' meaning if(not), or if(something is false)

 if(hit.rigidbody)
 {
     if(hit.collider.gameObject.tag == "AI")
     {
         //DoSomething
     }
 }

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Raycast Hit not toggling variable if stops hitting 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Inverse function of OnTriggerStay? 3 Answers

Object error 1 Answer

Detect OnMouseUp on gameobject with different colliders 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