- Home /
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#
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
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.
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);
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.
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.
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.
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?
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 ).
Your answer
Follow this Question
Related Questions
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