- Home /
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;
}
}
Have you double checked you have a prefab assigned to bulletfire
in the Inspector?
maybe you have to put the instance1 var on the top and then call it
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.
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)){
Your answer
Follow this Question
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