Trying to get Points to accumulate in game
I'm very new to Unity and I am trying to get a basic scoring system with shooting targets. I've got the score text working but it does not add any points. Each object is a basic cube with different point amounts added to each one and the Target script is attached. The Game Controller script is attached to the UI Text.
GameController.cs
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class GameController : MonoBehaviour {
Text text;
private int score;
// Use this for initialization
void Start () {
text = GetComponent <Text> ();
score = 0;
UpdateScore ();
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
text.text = "Score: " + score;
}
// Update is called once per frame
}
Target.cs
using UnityEngine; using System.Collections;
public class Target : MonoBehaviour {
public int scoreValue;
private GameController gameController;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider ci)
{
Destroy (gameObject);
Destroy (ci.gameObject);
gameController.AddScore (scoreValue);
}
}
When my bullet collides with the target I get an error in the console that says NullReferenceException: Object reference not set to an instance of an object Target.OnTriggerEnter (UnityEngine.Collider ci) (at Assets/Lab/Target.cs:31)
Your answer
Follow this Question
Related Questions
Audio Timing sync with Collision Object Appearance 0 Answers
How to make character controller not collide with specific object? 1 Answer
Is there an easy way to make a RigidBody2D collide with only one other RigidBody2D? 1 Answer
Best practive when removing pickup gameObject and its instances 0 Answers
How to move Raycast Hit Object 0 Answers