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 kiwiboys · Feb 10, 2014 at 05:39 AM · c#nullreferenceexception

NullReferenceException help

im not sure what the problem is all i want is for it to spawn the gameobject and add velocity to it but i keep getting this error here is the code

     using UnityEngine;
     using System.Collections;
     
     public class Fire_script : MonoBehaviour
     {
     
         public GameObject bullet;                // Prefab of the bullet.
         public float speed = 20f;                // The speed the bullet will fire at.
         
         
         private follow_crosshairs playerCtrl;        // Reference to the PlayerControl script.
     
     
         
         void Start()
         {
             
     
         }
         
         
         void Update ()
         {
             // If the fire button is pressed...
             if(Input.GetButtonDown("Fire1"))
             {
                 {
  Getting NullReferenceException: Object reference not set to an instance of an object Fire_script.Update () (at Assets/Fire_script.cs:29)?
 
 
                     // ... instantiate the rocket facing right and set it's velocity to the right. 
                     Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
                     bulletInstance.velocity = new Vector2(speed, 0);
             
         }
     }
         }
         }

i do have the bullet working it just doesn't add velocity it sort of just lands on the ground. please help if you can. C#

Comment
Add comment · Show 2
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 getyour411 · Feb 10, 2014 at 05:40 AM 0
Share

There aren't even 29 lines in the code as you have it formatted now. If you are going to post a code/help on error & line#, at least take care enough to format properly

avatar image kiwiboys · Feb 10, 2014 at 06:39 AM 0
Share

well it did want it to be descriptive and it's not meant to be 29 lines i spaced it out so it can be read.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Ambro · Feb 10, 2014 at 08:08 AM

Be careful, Instantiate function give you in return an Object that you can cast as a GameObject but not as a component.

If you want the component of a specific GameObject, you should use GetComponent function or built-in properties.

 Rigidbody2D bulletInstance = (GameObject)(Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,0)))).rigidbody2D;

or

 GameObject bulletInstance = (GameObject)Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,0)));
 bulletInstance.rigidbody2D.velocity = new Vector2(speed, 0);
Comment
Add comment · Show 7 · 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 kiwiboys · Feb 10, 2014 at 11:10 AM 0
Share

thank you this works well or though do you think you could make it fire towards the cursor. i have a script that allows the gun game object to follow the mouse cursor like a aim. the gun needs to be like thing thing (the game) i have the aim system working now it's just the fire script thats holding me up. don't know why this is taking me so long with so meany problems.

avatar image Ambro · Feb 10, 2014 at 01:26 PM 0
Share

If your fire script is attached to the weapon, you can use the rotation of your weapon as a starting rotation for your bullet.

Ins$$anonymous$$d of using Quaternion.Euler(new Vector3(0,0,0)) in the Instantiate function, you could use transform.rotation

The bullet will have the same rotation as your weapon, and physics should do the rest.

avatar image kiwiboys · Feb 11, 2014 at 02:24 AM 0
Share

hmm its seems to only be changing the rotation of the bullet game object and not the firing direction itself i have the scripted attached to an empty game object posited at the tip of the gun the game object is a child of the gun and should and does rotate with it but i have a hunch its something to do with the velocity maybe. the bullet always goes to the right no mater the rotation of the game object even if the player is facing left it just ends up shooting the player. see screen shot below. alt text

help fire.png (121.4 kB)
avatar image kiwiboys · Feb 11, 2014 at 05:12 AM 0
Share

yep im now pretty sure its to do with velocity i need to add it to forward but there is no forward z Axis on ridgedbody2D. any idea's?

avatar image Ambro · Feb 11, 2014 at 07:38 AM 0
Share

Try this :

 bulletInstance.rigidbody2D.velocity = (transform.right*speed);

It seems that Vector3 can be automatically converted back to Vector2, ignoring the Z-component.

Just replace transform.right by another axis if I picked the wrong one ( up/forward ).

Show more comments

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

NullReferenceException: Object reference not set to an instance of an object DestroyByContact.OnCollisionEnter2D (UnityEngine.Collision2D coll) 1 Answer

My Project doesn't work - Generic.List throws NullReferenceException ?! 0 Answers

Can't explain an NRE 0 Answers

NullReferenceException 1 Answer

Nested Class Initialization 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