0

I have 3 different scripts.

  • GameController
  • PlayerController
  • DestroyOnContact

GameController

public class GameController : MonoBehaviour {

public static Text scoreText;

void Start()
{
    StartCoroutine(SpawnWaves());
}

IEnumerator SpawnWaves()
{
    yield return new WaitForSeconds(startWait);
    while(true)
    {
        Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
        Quaternion spawnRotation = Quaternion.identity;
        Instantiate(hazardSmall, spawnPosition, spawnRotation);
        //SpawnHazard();
        yield return new WaitForSeconds(spawnWait);
    }
}

PlayerController

public static float score = 0;

DestoyOnContact

void OnTriggerEnter(Collider other)
{
    if (other.tag == "PlayerBolt")
    {
        PlayerController.score++;
        Debug.Log(PlayerController.score);
        GameController.scoreText.text = "Score: " + PlayerController.score;
        Debug.Log(GameController.scoreText.text);
    }
    Destroy(other.gameObject);
    Destroy(gameObject);

Problem

When I run my game and shoot the object that uses the DestroyOnContact script, the PlayerController.score++ works and I have debugged it. But trying to set the scoreText in GameController does not work. This results in neither the player shot nor the object being shot destroy.

I get the exception:

NullReferenceException: Object reference not set to an instance of an object

I have tried using:

GameObject gogc = GameObject.Find("GameController");
        GameController gc = (GameController)gogc.GetComponent(typeof(GameController));

Followed by:

gc.scoreText.text = "Score: " + PlayerController.score;

Which yields the same result. What am I missing?

Hierarchy:

Hierarchy as requested

derHugo
  • 83,094
  • 9
  • 75
  • 115
DannyBoy
  • 194
  • 1
  • 2
  • 13
  • Check if `GameController.scoreText` null with `Debug.Log` . – Programmer Oct 07 '17 at 14:25
  • It is null. However, if I try to set the text in the Start() it somehow stops all other code after setting text to not be executed. The game does not crash though. – DannyBoy Oct 07 '17 at 14:31
  • `GameController` is a class not a variable name right? If so:1. What's the name of the GameObject that the `Text` component is attached to? 2. What's the name of the GameObject that the `GameController` component is attached to? – Programmer Oct 07 '17 at 14:34
  • The GameController is a class. The scoreText is a public static class variable of GameController. The GameController does not have a GameObject because it's a "game mechanic" handling class so to speak. It works behind everything such as controlling UI. – DannyBoy Oct 07 '17 at 14:37
  • You have a Text component in the scene that you want to update, right? What's the name of the GameObject that the Text component is attached to? Please update your question with the `GameController` class since that seems like the problem. I need to see what it looks like. – Programmer Oct 07 '17 at 14:41
  • The text component is a child of "Canvas". Is that the gameObject I should be referring to? – DannyBoy Oct 07 '17 at 14:45
  • You are not answering the question that I've asked several times.This could have been solved long time ago but you unnecessary extending this. What is the name of that child Object the Text component is attached to? If you can't answer this, select it and post a screenshot of the Hierarchy tab. – Programmer Oct 07 '17 at 14:49
  • 1
    With your screenshot, the GameObject is named "Score". You have to initialize `scoreText` variable in the `GameController` script. Put this in the `Start` or `Awake` function of the `GameController` script.: `scoreText = GameObject.Find("Score").GetComponent();` – Programmer Oct 07 '17 at 14:57
  • This solved my problem. If you want to, add an answer so I can select it as solution. – DannyBoy Oct 07 '17 at 15:07
  • That's fine. I wasn't trying to add an answer since I consider it a duplicate. Was just trying to help. Glad I was able to help – Programmer Oct 07 '17 at 15:15

1 Answers1

0

I would recommend that you don't have the text as a static class as you then can't set it through the inspector.

Instead make a public static instance GameController.

Then in your Awake method of the GameController set instance = this;

You can then call that object by saying GameController.instance.ScoreText.text, remember that your ScoreText should NOT be static.

The reason i put it in the Awake method is to ensure it gets set early in the games lifecycle.

Feel free to ask questions.

Doh09
  • 2,324
  • 1
  • 16
  • 31