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 /
avatar image
0
Question by Tommo · Sep 07, 2012 at 11:11 AM · javascriptnullreferenceexceptionshooting

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

I get the NullReferenceException: Object reference not set to an instance of an object error at the moment and can not seem to find out why.

It says the problem is with this code:

 if (bullet)
     {
         Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.tranform.rotation);
         waitTillNextFire = 1;
     }


Here is how I declared them:

 var waitTillNextFire : float = 0;
 var bullet : GameObject;
 var bulletSpawn : GameObject;

And I have also put in the objects in the inspector as shown here: alt text

Here's the whole script :)

 var CameraObject : GameObject;
 @HideInInspector
 var targetXRotation : float;
 @HideInInspector
 var targetYRotation : float;
 @HideInInspector
 var targetXRotationV : float;
 @HideInInspector
 var targetYRotationV : float;
 
 var rotateSpeed : float = 0.3;
 
 var holdHeight : float = -0.5;
 var holdSide : float = 0.5;
 
 var ratioHipHold : float = 1;
 var hipToAimSpeed : float = 0.1;
 @HideInInspector
 var ratioHipHoldV : float = 1;
 
 var fireSpeed : float = 15;
 @HideInInspector
 var waitTillNextFire : float = 0;
 var bulletSpawn : GameObject;
 var bullet : GameObject;
 
 
 function Update ()
 {
     
     if (Input.GetButton("Fire1"))
     {
         if (waitTillNextFire <= 0)
         {
             if (bullet)
             {
                 Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.tranform.rotation);
                 waitTillNextFire = 1;
             }
         }
     }
         
         waitTillNextFire -= Time.deltaTime * fireSpeed;
         
         
         if (Input.GetButton("Fire2"))
         ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 0, ratioHipHoldV, hipToAimSpeed);
         if (Input.GetButton("Fire2") == false)
         ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 1, ratioHipHoldV, hipToAimSpeed);
         
         
         transform.position = CameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * ratioHipHold, holdHeight * ratioHipHold, 1));
         
         
         targetXRotation = Mathf.SmoothDamp( targetXRotation, CameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
         targetYRotation = Mathf.SmoothDamp( targetYRotation, CameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);
         transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
 }
inspector.png (1.7 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kryptos · Sep 07, 2012 at 11:55 AM

Does your screenshot represents the state of the inspector at the moment this error occured (during play mode) or just the state when editing?

It is possible that one of the two references become null during the game. Especially if you destroy one of them after the first shoot. You should keep a reference to the instantiated prefab so that you can destroy the instance when you don't need it anymore:

 private var usedBullet : GameObject;

 // ...
 usedBullet = Instantiate(...);
 
 // somewhere later...
 Destroy(usedBullet);
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 Tommo · Sep 07, 2012 at 12:05 PM 0
Share

Well that was taken during editing but its the same in play mode. I haven't put any code to destroy the bullet, it doesn't even move right now it just creates it at the bullet spawn... or it should.

Oh and the error only occurs when I click to fire.

avatar image
0

Answer by Cripple · Sep 07, 2012 at 12:32 PM

Can you share the entire script code please ? It may be related to the Fire call.

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

9 People are following this question.

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

Related Questions

"NullReferenceException" Error when trying to count objects with a particular tag using GameObject.FindGameObjectsWithTag function 1 Answer

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

Help with eval() 0 Answers

help with bullet shooting code? - javascript 1 Answer

Sidescroller Bullets Follow Mouse After Being Shot 2 Answers


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