• 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 sgmoney · May 03, 2015 at 09:49 PM · java

Why Does This Give a Reference Error

I am trying to create a class to have multiple kinds of guns but when i run this code and shoot it give my the error NullReferenceException: Object reference not set to an instance of an object FP_Shooting.Update () (at Assets/Scripts/FP_Shooting.js:139) please help. (the commented function is just a back up)

 #pragma strict
 
     var debrisPrefab : GameObject;
     var bullet_prefab : GameObject;
 
     var cam : Camera = Camera.main;
 
     var bulletImpulse : float = 100;
     var slot : float = 1;
     var cooldownRemaining : float = 0;
     var cool;
     var ran;
     var dam;
     var mo;
     var gunMode = Input.GetButtonDown;
 
 
 
 //defines guns
 
 var auto;
 
 public class Guns {
     public var debrisPrefab : GameObject;
     public var bullet_prefab : GameObject;
 
     public var cam : Camera = Camera.main;
 
     public var bulletImpulse : float = 100;
     public var slot : float = 1;
     public var cooldownRemaining : float = 0;
     public var cool :float ;
     public var ran : int;
     public var dam :int;
     public var mo : String;
     public var Shoot = function(){
 
         if ( cooldownRemaining <= 0){
             cooldownRemaining = this.cool;
             var ray : Ray = new Ray ( Camera.main.transform.position, Camera.main.transform.forward);
             var hitInfo : RaycastHit;
             if( Physics.Raycast(ray, hitInfo, this.ran)) {
                 var hitPoint : Vector3 = hitInfo.point;
                 var go = hitInfo.collider.gameObject;
             
             
                  var h : HasHealth;
                  h = go.GetComponent(HasHealth);
 
             
                 if(h != null){
                     h.TakeDamage(this.dam);
                 }
             
                 
                 }
             
                 if ( debrisPrefab != null && hitPoint != Vector3(0,0,0)){
                     MonoBehaviour.Instantiate( debrisPrefab, hitPoint, Quaternion.identity);
                 }
             }
 
         }; 
     
     
     public function Guns (cooldown : float, range : float, damage : float, mode : String){
         cool = cooldown;
         ran = range;
         dam = damage;
         mo = mode;
         Shoot();
     
     }
     public function Guns(){
         cool = 1;
         ran = 1;
         dam = 1;
         mo = "auto";
         Shoot();
     }
 }
 
 public var rifle : Guns = new Guns ( 0.1, 100, 10, "Auto");
 var currentGun = rifle;
 /* public function Shoot(){
 
     if ( cooldownRemaining <= 0){
         cooldownRemaining = cooldown;
         var ray : Ray = new Ray ( Camera.main.transform.position, Camera.main.transform.forward);
         var hitInfo : RaycastHit;
         if( Physics.Raycast(ray, hitInfo, range)) {
             var hitPoint : Vector3 = hitInfo.point;
             var go = hitInfo.collider.gameObject;
             
             
              var h : HasHealth;
              h = go.GetComponent(HasHealth);
 
             
             if(h != null){
                 h.TakeDamage(50);
             }
             
                 
             }
             
             if ( debrisPrefab != null && hitPoint != Vector3(0,0,0)){
             Instantiate( debrisPrefab, hitPoint, Quaternion.identity);
             }
         }
     } */
 
 
 
 function Start () {
     
 }
 
 function Update () {
 //define gun slots
 if (Input.GetButtonDown("Slot 2")){
     slot = 2;
     Debug.Log (slot);
     currentGun = rifle;
 }
 
 
 //Auto check
 
     if(currentGun.mo == "Semi"){
         gunMode = Input.GetButtonDown;
     }
     if(currentGun.mo == "Auto"){
         gunMode = Input.GetButton;
     }
 //guns and grenades fire
 
     if (gunMode ("Fire1")){
         rifle.Shoot();
     }
 
     cooldownRemaining -= Time.deltaTime;
     
     if (Input.GetButtonDown("Grenade")) {
         var theBullet : GameObject;
         theBullet = Instantiate (bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);
         theBullet.rigidbody.AddForce( cam.transform.forward * bulletImpulse, ForceMode.Impulse);
     }
     
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by siaran · May 03, 2015 at 10:44 PM

Seems like your rifle variable is null for some reason. Try adding some debug statements in the places where you set it and see where it gets set to null when it shouldn't. (and if it actually ever gets set to anything other than null).

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Smoothing Directionally between three materials 1 Answer

cannot create key in keystore 1 Answer

Help with Java socket server 1 Answer

How to extend UnityPlayerActivity 0 Answers

Accessing Data from a Android java class 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