Script Disabled upon creating a new GameObject

in this fonction:

    public void CreateEmptyHeart()
    {
        GameObject newHeart = Instantiate(heartPrefab, transform);
        newHeart.transform.SetParent(transform);

        HealthHeart heartComponent = newHeart.GetComponent<HealthHeart>();
        heartComponent.SetHeartImage(HeartStatus.Full);
        hearts.Add(heartComponent);
    }

i create a game object, the name of the GameObject is “Heart”(or “Heart(Clone)” and they have a script named “Health Heart” defined here:

using UnityEngine;
using UnityEngine.UI;

public enum HeartStatus 
{
    Empty=0,
    Half=1,
    Full=2
}

public class HealthHeart : MonoBehaviour
{
    public Sprite fullHeart, halfHeart, emptyHeart;
    // Tableau des images, indexé directement par la valeur de HeartStatus (0 = Empty, 1 = Half, 2 = Full)
    private Sprite[] heartSprites;
    Image heartImage;

    void Awake()
    {
        heartImage = GetComponent<Image>();
        heartSprites = new Sprite[] { emptyHeart, halfHeart, fullHeart };
    }

    public void SetHeartImage(HeartStatus status)
    {
    // Assignation de l'image sans vérification supplémentaire, en partant du principe que les valeurs sont sûres
        heartImage.sprite = heartSprites[(int)status];
    }
}

but once i start my scene, my Heart do not have the script turned off somehow. Please if anyone know


Here the proof a cloned Heart(created heart from my method)'s script is off

You do know Awake is called anyway right? see Unity - Scripting API: MonoBehaviour

The script is not already disabled on the prefab?

Otherwise, add an OnDisable method to the HealthHeart script containing a Debug.Log message. This should be called when the script is disabled after instantiation and the stack trace should give you a clue to what is disabling it.

1 Like
public class HealthHeart : MonoBehaviour
{
    public Sprite fullHeart, halfHeart, emptyHeart;
    // Tableau des images, indexé directement par la valeur de HeartStatus (0 = Empty, 1 = Half, 2 = Full)
    private Sprite[] heartSprites;
    public Image heartImage;

    void Awake()
    {
        heartImage = GetComponent<Image>();
        heartSprites = new Sprite[] { emptyHeart, halfHeart, fullHeart };
        SetHeartImage(HeartStatus.Full);
    }

    public bool SetHeartImage(HeartStatus status)
    {
        if (heartSprites == null || (int)status < 0 || (int)status >= heartSprites.Length)
        {
            return false; // Retourne false si l'index est hors limites ou si le tableau est null
        }

        // Assignation de l'image sans vérification supplémentaire, en partant du principe que les valeurs sont sûres
        heartImage.sprite = heartSprites[(int)status];
        return true; // Retourne true si l'opération est réussie
    }

With those slight modification and the inspector I confirmed that the bug come from the fact that the “Image” component is disabled, the script one being disabled is not a problem apparently.

Any idea how to:

  • Enable the Image component the moment the clone is created

The Hearts are not in the prefab, as they are fully created in the script of a grid Layout Group that has the script with the CreateEmptyHeart() Method.

In CreateEmptyHeart you instantiate the heartPrefab and the HealthHeart script is part of it? If the script is already disabled on heartPrefab, it will also be disabled on the instance you create. Same goes for the image component.

Components default to being enabled. If a component ends up being disabled, it either was already disabled before creating a copy with instantiate or something has disabled it. See my previous post on how you can try to find out what is disabling a script.

I found out my Issue, it is because I remove all Heart of the list just before drawing a new one, and the preFab is getting cleared. Thanks for helping me

1 Like