Hello everyone. I’m developing a shooting game with different types of bullets (pressing 1, 2, 3 and 4 will change them). I have a BasicBullet.cs which is like this
using UnityEngine;
using System.Collections;
public class BasicBullet : MonoBehaviour
{
public float speed = 5.0f;
public float DestroyTime = 10.0f;
public Vector3 velocity;
public void changeVelocity(Vector3 v)
{
velocity = v;
}
void Start ()
{
Destroy (gameObject, DestroyTime);
}
void FixedUpdate ()
{
// TO DO
transform.position += velocity * Time.deltaTime;
}
}
And my second bullet is like this
using UnityEngine;
using System.Collections;
public class Bullet2 : MonoBehaviour {
public float speed = 5.0f;
public float DestroyTime = 10.0f;
public Vector3 velocity;
public void changeVelocity(Vector3 v)
{
velocity = v;
}
void Start ()
{
Destroy (gameObject, DestroyTime);
}
void FixedUpdate ()
{
// TO DO
transform.position += velocity * Time.deltaTime;
}
}
which basically are the same, just for tests purposes.
Now, in my player shooting part, this is the code that I have
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour
{
public AudioClip shootAudio;
private AudioSource audioSource;
public GameObject bulletPrefab;
public GameObject fireParticle;
public float fireRate = 0.1f;
private float nextFire = 0.0f;
private bool shooting = false;
private int gunMode = 1;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update ()
{
//use mouse to fire
if (Input.GetButtonDown ("Fire1"))
shooting = true;
if (Input.GetButtonUp ("Fire1"))
shooting = false;
//use keyboard to change gun type
if (Input.GetKey("1"))
gunMode = 1;
else if(Input.GetKey("2"))
gunMode = 2;
else if(Input.GetKey("3"))
gunMode = 3;
else if(Input.GetKey("4"))
gunMode = 4;
if (shooting)
{
if(gunMode == 1 && Time.time > nextFire)
{
fireParticle.SetActive(false);
audioSource.PlayOneShot(shootAudio);
GameObject bullet = (GameObject)Instantiate(bulletPrefab,this.transform.position + this.transform.forward + this.transform.up, Quaternion.identity);
BasicBullet bulletComp = bullet.GetComponent<BasicBullet>();
bulletComp.changeVelocity(this.transform.forward * 10f);
nextFire = Time.time + fireRate;
}
else if(gunMode == 2)
{
fireParticle.SetActive(true);
}
//TO DO
else if(gunMode == 3 && Time.time > nextFire)
{
fireParticle.SetActive(false);
audioSource.PlayOneShot(shootAudio);
GameObject bullet = (GameObject)Instantiate(bulletPrefab,this.transform.position + this.transform.forward + this.transform.up, Quaternion.identity);
Bullet2 bulletComp = bullet.GetComponent<Bullet2>(); //<----PROBLEM!!!
bulletComp.changeVelocity(this.transform.forward * 10f);
nextFire = Time.time + fireRate;
}
else if(gunMode == 4)
{
}
}
else
{
if(gunMode == 2)
{
fireParticle.SetActive(false);
}
}
}
void OnTriggerStay(Collider other)
{
//for Flamethrower
if(gunMode == 2)
{
if (other.attachedRigidbody)
other.attachedRigidbody.AddForce(this.transform.forward * 50f);
}
}
}
but I get a NullReferenceException in here
Bullet2 bulletComp = bullet.GetComponent<Bullet2>();
bulletComp.changeVelocity(this.transform.forward * 10f);
nextFire = Time.time + fireRate;
What is the problem? Can anyone help me? Thanks!
The error is this one:
NullReferenceException: Object reference not set to an instance of an object
Shoot.Update () (at Assets/Scripts/Player/Shoot.cs:65)