I have two c# scripts.
One for the cannon:
using UnityEngine;
using System.Collections;
public class CannonScript : MonoBehaviour {
public GameObject bullet; //bullet object
public Transform spawnpoint; //spawn point
float bulletspeed=-3000f; //bullet speed
public int bullets=10; //number of bullets
void Update () {
if ((Input.GetKeyDown ("space") || Input.GetMouseButtonDown(0)) && bullets > 0) {
GameObject newbullet; //make a new bullet
newbullet=Instantiate(bullet,spawnpoint.position, spawnpoint.rotation)as GameObject; //instantiate the new bullet
newbullet.rigidbody.AddForce(spawnpoint.forward * bulletspeed); //add force to the new bullet
bullets--; //decrease the number of bullets left
}
}
}
Another for the UI text on the screen:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class BulletCountScript : CannonScript { //inherit from the CannonScript
Text text;
void Start () {
text = GetComponent<Text> ();
}
void Update () {
text.text = bullets.ToString(); //display the number of bullets
}
}
When I run it, the UI text on the screen never changes.
I know the CannonScript is working properly, because I can only shoot 10 bullets.
How can I make my UI text display the amount of bullets left?
Am I doing something wrong with inheritance?