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 MrSplosion · Jun 22, 2011 at 11:53 PM · raycastingfindwithtag

What's wrong with this script?

 var pickUpDistance : float;
 var cameraVar : Transform;
 
 private var hit : RaycastHit;
 
     function Update(){
         //debug ray
         Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.TransformDirection(Vector3.forward) * 100, Color.red);
         
         if(Input.GetButtonDown("Action")){
             PickUpWeapon();
         }
     }
     
     function PickUpWeapon(){
         //is the object within range?
         var rayVar = Physics.Raycast(cameraVar.transform.position, cameraVar.transform.forward, hit, pickUpDistance);
         if(rayVar){
             //is the object tagged "weapon"?
             if(hit.collider.GameObject.tag == "weapon"){
                 print(hit.collider.Gameobject.name);
             }
             else{
                 print("not a weapon");
             }
         }
         else{
             print("miss");
         }
     }



I get a NullReferenceException when I press my "action" button and the object is within range. This is the script I based it off of, but instead I'm finding the tag not the name:

 function Update () {
  
    var up = transform.TransformDirection(Vector3.up);
    var hit : RaycastHit;    
    Debug.DrawRay(transform.position, -up * 10, Color.green);
  
    if(Physics.Raycast(transform.position, -up, hit, 10)){
       Debug.Log("Hit");    
       if(hit.collider.gameObject.name == "floor"){
            Destroy(GetComponent(Rigidbody));
       }
    }
 }




I have no idea what I did wrong can someone help?!

Thanks!

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 SilverTabby · Jun 23, 2011 at 12:23 AM 1
Share

There are only 3 things that I can think of inside your code that could possibly cause a problem:

1)In Update(), you are using Camera.main.transform, but in PickUpWeapon(), you are using cameraVar. $$anonymous$$ake sure that cameraVar is assigned or just change it to Camera.main

2) In the unity scripting reference, it says that hit.collider can be null if the raycast didn't hit anything. Try if(hit.collider == null) print("miss"); else...

3) This should be fine, but it's bugging me: you're passing the raycast into a variable ins$$anonymous$$d of directly into an if statement like I normally see. I might be optimizing the null loop here, but try passing it directly into the if and avoiding the var rayVar = ....

avatar image Bunny83 · Jun 23, 2011 at 03:19 AM 0
Share

@$$anonymous$$rSplosion: Didn't you get compiler errors on that? Something like "GameObject is not part of UnityEngine.Collider".

@SilverTabby:

  1. Yes, if he assigned the var in the inspector it would work. It's not nice to use different approaches at the same time but it should work.

  2. That's kinda pointless as long as you check if you hit something. Physics.Raycast returns a boolean that tells you that and he's checking it.

  3. There's nothing wrong here. In this special case it's useless but in a lot cases you might need the result somewhere else.

2 Replies

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

Answer by Dreamblur · Jun 23, 2011 at 12:07 AM

These 2 lines:

      if(hit.collider.GameObject.tag == "weapon"){
             print(hit.collider.Gameobject.name);

should be:

      if(hit.collider.gameObject.tag == "weapon"){
             print(hit.collider.gameObject.name);
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 DaveA · Jun 23, 2011 at 12:08 AM 0
Share

Ya beat me to it!

avatar image save · Jun 23, 2011 at 12:16 AM 0
Share

You could also use the CompareTag() ins$$anonymous$$d of a string comparison which is a little bit faster.

avatar image MrSplosion · Jun 23, 2011 at 12:19 AM 0
Share

Ok thanks I cant belive u spotted that!

avatar image Bunny83 · Jun 23, 2011 at 03:23 AM 0
Share

I've missed it also the first time :D but that kind of flaws are quite regular recently, so we are kinda prepared :D

As I said you should get an compiler error on this issue...

avatar image
1

Answer by DaveA · Jun 23, 2011 at 12:08 AM

I'm guessing hit.collider.GameObject.tag should not capitalize gameObject

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

issues with raycasts 0 Answers

How to get the pixel depth values of the camera view? 0 Answers

Possible to use sprite mesh for UI.Image GraphicRaycaster culling? 1 Answer

nav mesh agent stop ScreenPointToRay after a while 0 Answers

What is the best way to raycast mutliple rays over a line 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