Score not updating dynamically!

Hello friends!

I have a problem where it seems that my PlayerObject is no longer able to visibly trigger a Score update onTrigger, and the score stays at 0 until the player dies, at which point the score changes to the actual points accrued-- but only until the next death, where it changes again. I would like this code to cause a visible update to the player as they get points, as per most games.

Thanks in advance!

James

CODE UPDATED TO REFLECT CURRENT TRIAL (22 July '15, @ 2219hrs)

Current ERRORS –

NullReferenceException: Object reference not set to an instance of an object
PlayerController.Start () (at Assets/Scripts/PlayerController.cs:17)

&

NullReferenceException: Object reference not set to an instance of an object
PlayerController.SetScoreText () (at Assets/Scripts/PlayerController.cs:45)
PlayerController.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/PlayerController.cs:37)

|||||-----|||||-----|||||-----|||||
GAME CONTROLLER HERE:
|||||-----|||||-----|||||-----|||||

using UnityEngine;
using System.Collections;

public class GameEngineScript : MonoBehaviour {

	public GameObject playerObject;		//player prefab
	public GameObject wallObject;		//wall prefab
	public GameObject scoreObject;		//blank square to increase score on collision
	public GameObject foregroundImage;	//image that is bottom wall of the game scene
	public GameObject backgroundImage;	//image that is background of the scene
	public GameObject HUD;				//player's UI
	public GameObject MainCamera;		//main game camera / scene view
	
	public float wallSeparation;	//space between top and bottom walls

	void Start () {
		InvokeRepeating ("CreateObstacle", 0f, 1.5f);	// calling "CreateObstacle" function after 0 seconds, then every 1.5 seconds
	}

	void Awake () {
		Instantiate (HUD); 				//player HUD		
		Instantiate (foregroundImage);	//spawn foreground
		Instantiate (playerObject); 	//spawn Player 
		Instantiate (backgroundImage);	//spawn Background
		Instantiate (MainCamera); 		//spawn camera
	}

	void CreateObstacle(){ 	//spawning obstacles and walls
		float randomPos = 6f - (7f - 0.8f - wallSeparation) * (Random.value); 					//generate random upper wall position

		GameObject upperWall = Instantiate (wallObject); 										//adding upper wall to stage
		upperWall.transform.position = new Vector2 (16f, randomPos); 							//setting upper wall position

		GameObject lowerWall = Instantiate (wallObject); 										//adding lower wall to stage
		lowerWall.transform.position = new Vector2 (16f, randomPos - wallSeparation - 4.8f);	//setting lower wall position

		GameObject scoreBlock = Instantiate (scoreObject); 										//adding score block to stage
		scoreBlock.transform.position = new Vector2 (16f, randomPos); 							//setting score block position
	}
}

|||||-----|||||-----|||||-----|||||
PLAYER CONTROLLER HERE:
|||||-----|||||-----|||||-----|||||

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour {

	public Vector2 jumpForce = new Vector2(); //Player Jump Force - controlled in inspector
	//public float restartDelay = 5f;  // Time to wait before restarting the level (PART OF LEVEL SELECTION || NOT YET IMPLEMENTED)

	private Text scoreText; //score text reference
	private int score; //score reference
	private Rigidbody2D rb; //rigidbody reference

	//float restartTimer;  // Timer to count up to restarting the level (DISABLED UNTIL DEATH AND LOAD LEVEL IS FIXED)
	
	void Start () {
		scoreText = GameObject.Find("HUD").GetComponentInChildren<Text>();
		score = 0; //start score at zero
		rb = GetComponent<Rigidbody2D>(); //initialize rigidbody
		SetScoreText ();
	}
	
	void Update () {
		if (Input.GetButtonDown ("Fire1")) { 		//waiting for player input (mouse)
			GetComponent<Rigidbody2D>().velocity = Vector2.zero; //setting the player's rigid body velocity to zero
			GetComponent<Rigidbody2D>().AddForce(jumpForce); //adding jump force to player's rigid body
		}
		Vector2 stagePos = Camera.main.WorldToScreenPoint (transform.position);
		if (stagePos.y > Screen.height || stagePos.y < 0) {
			die(); //...call death function
		}
	}
	
	void OnTriggerEnter2D(Collider2D other) { //score when collide point block & death when collide wall block
		if (other.gameObject.CompareTag ("PointObject")){
			score = score + 1;
			SetScoreText ();
		}
		if (other.gameObject.CompareTag ("Wall")) {
			die ();
		}
	}
	
	void SetScoreText (){ 	//setting count text reference
		scoreText.text = "Points: " + score;
	}
	
	void die(){ 	//reload the current scene (restart game)
		//COMMENT THIS OUT OR REMOVE ONCE RESTART DELAY HAS BEEN PROPERLY IMPLEMENTED WITH GAME OVER SPLASH + CHARACTER DEATH
		Application.LoadLevel (Application.loadedLevel);
		//increments timer to count up to restarting (internal)
		//restartTimer += Time.deltaTime;

		//...if it reaches restart delay, game reloads
		//if (restartTimer >= restartDelay) 
		//{
		//	Application.LoadLevel (Application.loadedLevel);
		//}
	}
}

I believe you have no reference to a scoring object only the reference to the prefab to create one so if your instantiate you would do.

Text scoreText = Instantiate(ScoreText,pos,rot) as Text.

now you are changing scoreText from a refrence to a prefab to a refrence of an instantiated object.
if this works then you can fiddle with it how you want so you could make a new veriable to hold the new object so you dont override the prefab.

Edit

Okay i think i see your set up now from your video, youe Hud is instantiated by the GameEngine Object and its the Hud object that has the text you want so

Step 1) Change your scoreText to private

Step 2) Add this Line of code to your start function

scoreText = GameObject.Find("HUD").GetComponentInChildren<Text>();

Bonus Step: if you are getting null exception issues try putting your instantiate code into Void Awake() so it is finished before Void Start() runs

Mmm one solution would be to put SetScoreText() in the update but i guess that’s not what you’re expecting ^^

Otherwise:

  • make sure object you pick has a Collider2D,
  • and has the good tag,
  • that your character’s collider is Trigger,
  • that the tag is spelt correctly in the script