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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Bodaciouslycrazy · Apr 23, 2015 at 05:30 PM · raycastnullreferenceexceptionobject referenceout

Raycasting error

Hello Everyone! I am trying to learn some basic raycasting to make some bullet stuff. The Player script has a public method that returns true if a ray directly infront of the camera hits something. It also outs a RaycastHit variable. Within the weapon script, I am trying to make it use that function to detect if the ray hit something. If not, it creates a bullet object (bullet objects don't actually do anything, they are there as a visual queue so that the player knows they have shot something) directly from the gun going forward at the max distance that the gun shoots. If the ray does hit something and returns true, the weapon script will make the bullet angle toward the hit point so that it looks more accurate without getting infront of the player's face and blocking the camera. But I am having a weird issue with it saying...

"Object reference not set to an instance of an object"

...when it calls the sendRay() method.

Here is the scripts...

First, the player script, which is a component of the main camera.

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
     
     public bool sendRay(out RaycastHit hit, float maxDist){
         Ray shot = new Ray(transform.position, transform.forward);
         if (Physics.Raycast(shot, out hit, maxDist)) return true;
         else return false;
     }
 }
 

here is the weapon script, on a component of an object that is a child of the main camera.

 using UnityEngine;
 using System.Collections;
 
 public class MachineGun : MonoBehaviour {
     
     public float fireDelay = 0.2f;
     public int damage = 1;
     public float maxDistance = 10.0f;
     float currentDelay = 0.0f;
     
     public GameObject bullet;
     
     // Update is called once per frame
     void Update () {
         currentDelay -= Time.deltaTime;
         if(currentDelay < 0.0f)
             currentDelay = 0.0f;
         
         if(Input.GetAxis("Fire1") == 1.0f && currentDelay == 0.0f){
             
             GameObject bul = Instantiate(bullet, transform.position, transform.rotation) as GameObject;
             bul.GetComponent<Bullet>().setDistance( maxDistance );
             RaycastHit hit;
             
             if( transform.parent.GetComponent<Player>().sendRay(out hit, maxDistance) ){
                 
                 bul.transform.LookAt(hit.point);
                 bul.GetComponent<Bullet>().setDistance( 1.0f );
                 
                 if(hit.collider.tag == "Enemy"){
                     hit.collider.gameObject.GetComponent<Health>().hurt(damage);
                 }
                 
             }
         }
     }
 }


The specific error is:

NullReferenceException: Object reference not set to an instance of an object MachineGun.Update () (at Assets/Custom/Weapons/MachineGun.cs:25)

What do you think would cause this problem, and how can I fix it?

Comment
Add comment · Show 6
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 maccabbe · Apr 23, 2015 at 05:44 PM 0
Share

This is not a raycasting error but a null reference error and you can find many similar questions. Check the stuff used on line 26 (the line the error occurs on) against null.

avatar image Chris333 · Apr 23, 2015 at 09:07 PM 0
Share

Did you assigned a gameObject to the bullet variable?

avatar image Bodaciouslycrazy · Apr 23, 2015 at 10:16 PM 0
Share

@ $$anonymous$$accabbe; I think I got that far. I just don't know what to do to solve it. I think it might be the fact that a method can't use an outed variable as a parameter until it has a value? I don't really know, I am new to C#.

@ Chris333; Yes, there is a prefab object assigned there.

avatar image Bodaciouslycrazy · Apr 23, 2015 at 10:17 PM 0
Share

I also want to mention, there was a glitch that was making the bullet fire on every frame ins$$anonymous$$d of the actual fire rate. Although that is unrelated to this problem, I did fix it.

avatar image Chris333 · Apr 23, 2015 at 10:55 PM 0
Share

Could you try this:

if(transform.parent.gameObject.GetComponent().sendRay(out hit, maxDistance) )

in line 25.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bodaciouslycrazy · Apr 24, 2015 at 03:51 AM

OH my gosh, I feel like an idiot. I wasted everyone's time... I made this player script, but completely forgot to actually place it on the main character. Such a stupid mistake. I thought it had to do with "out" variables... Thank you for helping though! I am learning a lot!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

RaycastHit2D.collider is null - why? 1 Answer

LitJson not working 0 Answers

NullReferenceException: Object reference not set to an instance of an object 2 Answers

NullReferenceException: Object reference not set to an instance of an object ss.Update () (at Assets/ss.cs:21) 0 Answers

nullreference exeptio, object reference not set to an instance of an object 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