0

I'm attempting to change a UI element of the canvas using TextMeshPro and 2 scripts. I have a function that works at the end of a class that destroy the game object of one of the players (its local multiplayer), finds which player it is and attempts to lead to a Score script. Up till that point it works and the check for player works as it prints a debug showing who won that round, however I'm getting an error telling me

NullReferenceException: Object reference not set to an instance of an object Tank.Destroy () (at Assets/Scripts/Tank.cs:147)

Here's the Destroy function, it works all the way through and fails at the attempt to link to PlayerOne/PlayerTwoScored

 public void Destroy()
     {

         Debug.Log(this.gameObject + " has died" );
         Destroy(this.gameObject);
         GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
         Destroy(effect, 10f);
         SceneManager.LoadScene(Application.loadedLevel);

         Score s = gameObject.GetComponent<Score>();
         if (this.gameObject.name.Contains("Tank Player 1"))
         {
             Debug.Log("Player Two wins!");
             s.PlayerTwoScored();
         }
         else if (this.gameObject.name.Contains("Tank Player 2"))
         {
             Debug.Log("Player One wins!");
             s.PlayerOneScored();
         }
         else { return; }

     }

Here's the code for the score script

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using TMPro;

 public class Score : MonoBehaviour
 {
     public int scoreValue1 = 0;
     public int scoreValue2 = 0;
     public TMP_Text score1;
     public TMP_Text score2;

     public void PlayerOneScored() {
         Debug.Log("Player One Scored");
         score1 = GetComponent<TMP_Text>();
         score1.text = "P1 Score: " + scoreValue1;

     }

     public void PlayerTwoScored()
     {
         Debug.Log("Player Two Scored");
         score2 = GetComponent<TMP_Text>();
         score2.text = "Score: " + scoreValue2;
     }
 }

Included I have a picture of the log, and the UI showing the script and the TMP text objects in the canvas.

UI

Log

Update : I checked up other question threads and assumed that my problem with this was the fact that i used

Score s = gameObject.GetComponent<Score>();

When i should have used

Score s = new Score();

As Score is not attached to the same gameObject but a UI element (The canvas and the 2 Text files as seen in image). I'm not sure how to associate it with this. I assumed my above code was correct at the time but i am now getting this error

NullReferenceException UnityEngine.Component.GetComponent[T] () (at C:/buildslave/unity/build/Runtime/Export/Component.bindings.cs:42) Score.PlayerOneScored () (at Assets/Scripts/Score.cs:16) Tank.Destroy () (at Assets/Scripts/Tank.cs:147)

Line 16 in Score is

score1 = GetComponent<TMP_Text>();

And line 147 in Tank is

s.PlayerOneScored();

ConnnK
  • 19
  • 1
  • 8

0 Answers0