How do I update the UI of an Instantiated object?

I have a missile that get instantiated in my player class. In my missile class I have an OnTriggerEnter2D function I am trying to update the score. It seems that the score doesn’t update outside of the OnTriggerEnter2D so it never reaches the score UI. Ive tried to have the score += 1; inside the ontriggerenter function as well as making a bool and neither work. please help

Here is the code for the missile class:

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

public class Missile : MonoBehaviour
{

    private Player player;
    Vector3 screenBounds;

    public GameObject mediumAsteroid;
    public GameObject smallAsteroid;

    public bool largeDestroyed;
    public bool mediumDestroyed;
    public bool smallDestroyed;

    public float score = 0;
    public Text t_score;

    private void Start()
    {
        t_score = GameObject.Find("Score").GetComponent<Text>();
        t_score.text = "Score: ";
    }

    private void Update()
    {
        screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));

        /*if (transform.position.y > screenBounds.y || transform.position.y < -screenBounds.y || transform.position.x > screenBounds.x || transform.position.x < -screenBounds.x)
        {
            Destroy(gameObject);
        }
        */

        t_score.text = "Score: " + score.ToString();

        if (largeDestroyed)
        {
            score += 20;
        }

        if (mediumDestroyed)
        {
            score += 50;
        }

        if (smallDestroyed)
        {
            score += 100;
        }

        //Debug.Log("Points " + score);

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.isTrigger != true && collision.tag != ("Player"))
        {
            if(collision.CompareTag("LargeAsteroid"))
            {
                collision.GetComponent<Asteroid>().DestroyAsteroid();
                Instantiate(mediumAsteroid, new Vector3(transform.position.x - .5f, transform.position.y - .5f, 0), Quaternion.Euler(0, 0, 90));
                Instantiate(mediumAsteroid, new Vector3(transform.position.x + .5f, transform.position.y + .5f, 0), Quaternion.Euler(0, 0, 0));
                largeDestroyed = true;
            }
            if (collision.CompareTag("MediumAsteroid"))
            {
                collision.GetComponent<Asteroid>().DestroyAsteroid();
                Instantiate(smallAsteroid, new Vector3(transform.position.x - .5f, transform.position.y - .5f, 0), Quaternion.Euler(0, 0, 90));
                Instantiate(smallAsteroid, new Vector3(transform.position.x + .5f, transform.position.y + .5f, 0), Quaternion.Euler(0, 0, 0));
                mediumDestroyed = true;
              
            }
            if (collision.CompareTag("SmallAsteroid"))
            {
                collision.GetComponent<Asteroid>().DestroyAsteroid();
                smallDestroyed = true;
            }


            Destroy(gameObject);
        }
    }

}

You have to think 4 dimensionally, or rather sequentially, you have to set your score value before you you set the text value. so put this line: t_score.text = "Score: " + score.ToString(); at about line 54.

Several things:

  1. Make sure you set your score before you destroy the GameObject. That means, try setting the score inside the OnTriggerEnter2D, before you call Destroy, not in Update.

  2. If you want a global score count, don’t use a score variable inside the Missile class. In this code, Missile does not know how many rocks you’ve shot down before and how many points you’ve collected up until now. Rather create a Score class (maybe place it somewhere on you UI GameObjects) and give it a public void AddPoints(int newPoints) method that adds the newPoints to an internal score variable and updates the UI. Call that method inside Missile’s OnTriggerEnter2D.