title: How to avoid object references for spawned objects in Unity 2D?
body: <p>At the moment I am programming a Unity 2D game. When the game is running the cars start moving and respawn continuously. I added kind of a life system to enable the possibility to shoot the cars. My issue is that my health bar as well as my score board need references to the objects they refer to, but I am unable to create a reference to an object which is not existing before the game starts. Another issue is that I don't know how to add a canvas to a prefab in order to spawn it with the cars continuously and connect them to the car. Is there a way to avoid these conflicts or how is it possible to set the references into prefabs. I will add the code of the spawner, the car and the the scoreboard. Already thank you in advance</p>
  
  <p>Spawner:</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Spawner : MonoBehaviour
  {
    public GameObject carPrefab;
    public GameObject enemyCarPrefab;
    public GameObject kugel;
    public float respawnTime = 10.0f;
    public int counterPlayer1=0;
    public int counterPlayer2=0;
    public int counterEnergy=0;
  
    // Use this for initialization
    void Start () {
        StartCoroutine(carWave());
    }
    private void spawnPlayerCars(){
        GameObject a = Instantiate(carPrefab) as GameObject;
        a.transform.position = new Vector2(-855f, -312.9426f);
        counterPlayer1++;
    }
    private void SpawnEnemyCars(){
        GameObject b = Instantiate(enemyCarPrefab) as GameObject;
        b.transform.position = new Vector2(853,-233);
        counterPlayer2++;
    }
    private void SpawnEnergy(){
        GameObject c = Instantiate(kugel) as GameObject;
        c.transform.position = new Vector2(-995,-192);
        counterEnergy++;
    }
  
  
    IEnumerator carWave(){
        while(true){
            yield return new WaitForSeconds(respawnTime);
            if(counterPlayer1&lt;3){
            spawnPlayerCars();
            Debug.Log(counterPlayer1);
            }
            if(counterPlayer2&lt;3){
            SpawnEnemyCars();
            Debug.Log(counterPlayer2);
            }
            if(counterEnergy&lt;3){
            SpawnEnergy();
            }
        }
    }
  
  
  }
  </code></pre>
  
  <p>Car:</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class EnemyCar : MonoBehaviour
  {
      public float speed = 3f;
      int zählerAuto1=0;
      private Vector2 screenBounds;
      public AnzeigePunktzahlPlayer2 points;
      public Spawner sp;
      public int maxHealth=100;
      public int currentHealth;
      public HealthBar healthbar;
  
      void Start () {
      screenBounds = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
      points= GetComponent&lt;AnzeigePunktzahlPlayer2&gt;();
      sp= GetComponent&lt;Spawner&gt;();
      currentHealth=maxHealth;
      healthbar.SetMaxHealth(maxHealth);
  
  
  
      }
      void Update()
      { 
      Vector2 pos = transform.position;
          if(pos.x&gt;-855f){
          pos = transform.position;
          pos.x-= speed* Time.deltaTime;
          transform.position=pos;
          zählerAuto1++;
          }else{
              points.counter++;
              Debug.Log(points.counter);
              sp.counterPlayer2--;
              Debug.Log(sp.counterPlayer2);
              Destroy(this.gameObject);        
          }
      }
      private void OnCollisionEnter2D(Collision2D other) {
          if (other.collider.tag=="Kugel"){
              takeDamage(40);
              //sp.counterPlayer2--;
  
              if(currentHealth&lt;=0)
              {
              Destroy(this.gameObject);
              }
          }
  }
  public void takeDamage(int damage){
      currentHealth-= damage;
      healthbar.SetHealth(currentHealth);
  }
  
  public void getHealed(int heal){
      currentHealth+= heal;
      healthbar.SetHealth(currentHealth);
  }
  }
  </code></pre>
  
  <p>Scoreboard(one part of it(the other one is almost the same)):</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using TMPro;
  
  public class AnzeigePunktzahlPlayer1 : MonoBehaviour
  {
      public int counter;
      public TextMeshProUGUI textPlayer1;
  
      void Start()
      {
   //   counter=0; 
      textPlayer1= GetComponent&lt;TextMeshProUGUI&gt;();
      }
  
      // Update is called once per frame
      void Update()
      {
          textPlayer1.SetText( counter.ToString());
      }
  }
  
  
  
  </code></pre>