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 Syllith · Jul 25, 2011 at 04:01 AM · instantiateraycastcollidernullreferenceexceptionnull

Raycast causes game to crash when shooting up

Okay, so right now I have a gun script that raycasts directly in front of the camera when the character shoots. Right now, it works fine when I shoot at something, but if I just look up and try to shoot at the sky the game crashes and says "NullReferenceException: Object reference not set to an instance of an object" even though I've used the same script several times without a problem. Here's what I have so far:

 var direction = this.transform.TransformDirection(Vector3.forward);
 var RayCastHit : RaycastHit;
 if (Physics.Raycast (transform.position, direction, RayCastHit, FireRange)) 
     {
         if (RayCastHit.rigidbody)
         {
             if (Weapon == "G36C")
             {
                     RayCastHit.rigidbody.AddForceAtPosition(150 * direction, RayCastHit.point);
             }
         }
     }
     if (RayCastHit.collider.gameObject.tag == "metal") 
         {                
             MetalParticle1.transform.position = RayCastHit.point;
             MetalParticle2.transform.position = RayCastHit.point;
             MetalParticle3.transform.position = RayCastHit.point;
             MetalParticle4.transform.position = RayCastHit.point;
             MetalParticle5.transform.position = RayCastHit.point;
             MetalParticle1.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             MetalParticle2.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             MetalParticle3.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             MetalParticle4.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             MetalParticle5.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             MetalParticle1.Emit();
             MetalParticle2.Emit();
             MetalParticle3.Emit();
             MetalParticle4.Emit();
             MetalParticle5.Emit();
             MetalParticle1.audio.Play();
             }
         else if (RayCastHit.collider.gameObject.tag == "concrete")
         {
             ConcreteParticle1.transform.position = RayCastHit.point;
             ConcreteParticle2.transform.position = RayCastHit.point;
             ConcreteParticle3.transform.position = RayCastHit.point;
             ConcreteParticle4.transform.position = RayCastHit.point;
             ConcreteParticle1.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             ConcreteParticle2.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             ConcreteParticle3.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             ConcreteParticle4.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             ConcreteParticle1.Emit();
             ConcreteParticle2.Emit();
             ConcreteParticle3.Emit();
             ConcreteParticle4.Emit();
             ConcreteParticle1.audio.Play();
         }
         else if (RayCastHit.collider.gameObject.tag == "wood")
         {
             WoodParticle1.transform.position = RayCastHit.point;
             WoodParticle2.transform.position = RayCastHit.point;
             WoodParticle3.transform.position = RayCastHit.point;
             WoodParticle4.transform.position = RayCastHit.point;
             WoodParticle1.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             WoodParticle2.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             WoodParticle3.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             WoodParticle4.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             WoodParticle1.Emit();
             WoodParticle2.Emit();
             WoodParticle3.Emit();
             WoodParticle4.Emit();
             WoodParticle1.audio.Play();
         }
         else if (RayCastHit.collider.gameObject.tag == "water")
         {
             WaterParticle1.transform.position = RayCastHit.point;
             WaterParticle2.transform.position = RayCastHit.point;
             WaterParticle3.transform.position = RayCastHit.point;
             WaterParticle1.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             WaterParticle2.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             WaterParticle3.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             WaterParticle1.Emit();
             WaterParticle2.Emit();
             WaterParticle3.Emit();
         }    
             else 
         {
             DirtParticle1.transform.position = RayCastHit.point;
             DirtParticle2.transform.position = RayCastHit.point;
             DirtParticle3.transform.position = RayCastHit.point;
             DirtParticle1.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             DirtParticle2.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             DirtParticle3.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
             DirtParticle1.Emit();
             DirtParticle2.Emit();
             DirtParticle3.Emit();
             DirtParticle1.audio.Play();
         }

When I double click the debug thing at the bottom, it highlights the line that says "if (RayCastHit.collider.gameObject.tag == "metal")".

Any help would be appreciated. Thanks

Comment
Add comment · Show 4
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 Dreamblur · Jul 25, 2011 at 04:08 AM 1
Share

"Shooting at the sky" means that the member collider in your RaycastHit will remain uninitialized. An uninitialized Collider will not have an attached GameObject or tag. So yes, you have a null reference.

(Posted as a comment because you don't accept answers.)

avatar image Syllith · Jul 25, 2011 at 04:14 AM 0
Share

I have the same code on a different game and it works perfectly fine. What could be causing it not to work?

avatar image Dreamblur · Jul 25, 2011 at 04:17 AM 1
Share

I just told you what the problem is. When the first if-conditional is reached, the game tries to access RayCastHit.collider.gameObject.tag and finds out that it is non-existent, causing it to crash.

avatar image Syllith · Jul 25, 2011 at 04:46 AM 0
Share

But why does my other script work fine?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Jul 25, 2011 at 04:35 AM

You may have changed something in this script, because you're closing the first if before start checking the hit object's tag, so you're comparing the tags even if Raycast returns false (what happens when you shot the sky). Move the 3rd closing brace to the end of your script. You can also do a lot of optimization, like below (pay attention to the comments):

var direction = this.transform.TransformDirection(Vector3.forward); // transform.forward does the same var RayCastHit : RaycastHit; if (Physics.Raycast (transform.position, direction, RayCastHit, FireRange)) { if (RayCastHit.rigidbody) { if (Weapon == "G36C") { RayCastHit.rigidbody.AddForceAtPosition(150 * direction, RayCastHit.point); } } // } <- move this brace to the end of your script // you can do a lot of optimization caching the rotation and tag: var rotNormal = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal); var hitTag = RayCastHit.collider.tag; // you can access tag from the collider reference if (hitTag == "metal") // use the cached tag to compare { MetalParticle1.transform.position = RayCastHit.point; MetalParticle2.transform.position = RayCastHit.point; MetalParticle3.transform.position = RayCastHit.point; MetalParticle4.transform.position = RayCastHit.point; MetalParticle5.transform.position = RayCastHit.point; MetalParticle1.transform.rotation = rotNormal; // use the calculated rotation MetalParticle2.transform.rotation = rotNormal; MetalParticle3.transform.rotation = rotNormal; MetalParticle4.transform.rotation = rotNormal; MetalParticle5.transform.rotation = rotNormal; MetalParticle1.Emit(); ...

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 Syllith · Jul 25, 2011 at 04:53 AM 0
Share

Ahhhh, thank you so much! I actually suspected something like that but I wasn't sure.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

What would be a good way to not have objects spawn inside one another? 2 Answers

null new collider for raycast grabbing 1 Answer

Instantiate gameObject returns null? 1 Answer

Stop platforms spawning inside player 1 Answer

Get an Object to Face/Move to RayCast Hit.point? 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