Assigning a script from a gameobject to a component in a prefab.

Hello, new to unity and coding. I have prefabs that I spawn into the scene, and touching them makes the player take damage. I have a component in my “DamageControl” script that calls a script from a gameobject that will have the player lose health, this gameobject also controls the text that shows the player how much health they have.

The problem is that I have no idea how to put this gameobject into the prefab upon starting the game. Setting the gameobject as a prefab doesn’t work because then the text doesn’t update, nor does the game end when the players health reaches 0.

Here is the code for the scripts that I am working with:

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

public class DamageControl : MonoBehaviour
{
   [SerializeField] public float circleDamage;
   [SerializeField] private HealthControl HealthController;
   

    void Start() {      
      
    }

   private void OnTriggerEnter2D(Collider2D collision)
    {
    if(collision.CompareTag("Player"))
    {
        Damage();
    }
   } 
    void Damage()
    {
      HealthController.playerHealth = HealthController.playerHealth - circleDamage; 
      HealthController.UpdateHealth();
      this.gameObject.SetActive(false); 
    }
}

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

public class HealthControl : MonoBehaviour
{
    public float playerHealth;
    [SerializeField] private Text healthText;
    public void UpdateHealth()
    {
        healthText.text = playerHealth.ToString("0");
    }

    private void Update() {
         if(playerHealth == 0)
        {
            SceneManager.LoadScene("gameover");
        }
    }
   
}

Thank you.

From what I understand, when your player hits enemy, player takes damage and enemy disappears.

When dealing with classes, you must understand the purpose of each class. For instance, your prefab has DamageControl component (class), then you have gameObjectwith HealthControland finally player (or is gameObject your player?). But the thing is, why scripts, that that control player are outside of player? Why mere prefab has the authority to control damage? You see where this is going.

So let’s add to our player PlayerController script:

public class PlayerController : MonoBehavior
{
    [SerializedField] private float _playerHealth; //serialization here for it to be visible in inspector
    [SerializedField] private Text _healthText;
    public Text GetText => _healthText;

    public void RecieveDamage(float damage)
    {
        _playerHealth = Mathf.Max(0, _playerHealth - damage);
    }

    // And here method for updating health text
} 

Then rename class on prefab to something like Enemy. This class will have [SerializedField] private float _damage. And after checking collision with tag, you can do

PlayerController player = collision.GetComponent<PlayerController>();
player.RecieveDamage(_damage);
player.UpdateText();
...

That’s basically it. There may be some errors, 'cause I am writing from mobile phone, but nothing linter in your IDE won’t see.

You may have to make reference to the script first
*

public TheScriptYouWantToAdd scriptToAdd; // Drag it in the inspector
  • gameObject.AddComponent();