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 averkom · Dec 29, 2013 at 10:36 AM · javascriptinstantiateprojectilestandalone

Object reference not set to an instance of an object

I'm currently getting a problem with using instantiate to fire a projectile, it works fine in the editor but not once built as webplayer or standalone.

The error I'm getting in the output log is:

**NullReferenceException: Object reference not set to an instance of an object at Elements.Update () [0x00000] in :0

(Filename: Line: -1)**

I have had a look at other examples with this happening but had no luck with implementing it into my code. Any help with sorting this out would be appreciated.

 #pragma strict
     
     static var element : int = 1;
     
     
     var bulletfire : Rigidbody;
     var bulletice : Rigidbody;
     var power : float = 1500;
     var damage : float = 100;
     var bulletcount : int = 10000;
     var gunshot: AudioClip;
     var allowfire : int = 1;
     
     
     function Start () {
       bulletcount = bulletcount;  
         
     
     }
     
       
     function checkgun () {  
       yield WaitForSeconds (0.5);
         allowfire = 1;
       }
     
     
     
     function Update () {
          
          
          
          
          if (Input.GetKeyDown ("1")){
                 element = 1;
         }
         
         if (Input.GetButton("Fire1")&& (allowfire == 1) && bulletcount && (element == 1)){
         
         allowfire = 0;
         audio.PlayOneShot(gunshot);
         var instance1: Rigidbody = Instantiate(bulletfire, transform.position, transform.rotation) as Rigidbody;
         var fwd1: Vector3 = transform.TransformDirection(Vector3.forward);
         instance1.AddForce(fwd1 * power); 
         bulletcount --;
         checkgun();
         }
         
     
         if (Input.GetKeyDown ("2")){
                 element = 2;
         }
         
     }
     
 

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 KellyThomas · Dec 29, 2013 at 09:52 AM 0
Share

Have you double checked you have a prefab assigned to bulletfire in the Inspector?

avatar image averkom · Dec 29, 2013 at 10:16 AM 0
Share

Just checked and it's assigned.

avatar image AmazingB · Jan 04, 2014 at 02:01 AM 0
Share

maybe you have to put the instance1 var on the top and then call it

avatar image kromenak · Jan 04, 2014 at 03:43 AM 0
Share

You might want to add some Debug.Log statements to see what exactly is set to null. gunshot, bulletfire, transform, or instance1 could possibly generate that error if they were set to null for some reason.

If instance1 is null, then the Instantiate method is failing for some reason. If the other two are null, it means that the inspector values are not set for some reason. If transform is null...that is very strange.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SnStarr · Jan 10, 2015 at 10:02 AM

Its not because the variable of bulletCount is being checked for true in the if statement block for the Input of Fire 1, yet its a Var not a boolean value. Wouldnt it have to be set like if( bulletCount == something) since its a Var? Or remove the variable of bulletCount from the If statement block altogether, cause it looks unnecessary to me or at least make it check whether or not bulletCount is > 0 to allow Shoot. With the code as it looks to me anyway, its check to see IF Fire 1 was pressed, and since bulletCount variable was set to 10000, then its basically like saying

 if(Input.GetMouseButtonDOwn(0) &&(AllowFire = 1) && 10000 && (element=1)) 
 { 
 then run this code 
 }


That 10000 in there isn't needed. Change that line of code to this:

 if (Input.GetButton("Fire1")&& (allowfire == 1) && bulletcount > 0 && (element == 1)){










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

21 People are following this question.

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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Instantiating a random dropped consumable item from many cloned objects 1 Answer

2D Space Shooter Instantiating bullet question 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 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