Ok, I am following the unity 3D tutorials from the official website, however I coding my own stuff as I go rather than just copying the code from the videos. The ideas is the following. I have a script where I hold data as ammo, bombs and so on. I have a spaceship shooting perfectly, but I want the ammo to decrease so I am using GetComponent to access the script holding the data and therefore be able to decrease it, however when I am going to test the code, I get a nullreferenceexception and I can't figure out what it is.
using UnityEngine;
using System.Collections;
public class Shooting : MonoBehaviour
{
public GameObject laserPrefab;
public Transform guns;
private Inventory inventory;
//private float laserSpeed = 50;
// Use this for initialization
void Awake()
{
inventory = GetComponent();
}
void Start()
{
}
// Update is called once per frame
void Update ()
{
shoot();
}
void shoot()
{
if(Input.GetKeyDown(KeyCode.Space))
{
Instantiate(laserPrefab, guns.position, guns.rotation);
inventory.ammo.bullets--; //This is the problem
Debug.Log(inventory.ammo.bullets);
}
}
}
Any help will be appreciated, thank you very much for your time!
↧