Hello guys,
I have found a weird behavior in my script where I get the follow error:
This is the stack trace:
This is How I instantiate my “destroyed” object:
private void InitializeVitalBar(GameObject vitalsContainer, VitalType vitalType, Vector3 pos)
{
var bar = Resources.Load<FloatingVitalBar>($"UI/Vitals/Floating{vitalType.ToString()}");
var barInstance = Instantiate(bar, pos, Quaternion.identity);
Debug.Log(barInstance == null);
barInstance.Init(_context, _playerId, vitalType);
barInstance.transform.SetParent(vitalsContainer.transform);
}
And this is my object:
public class FloatingVitalBar : MonoBehaviour
{
private Guid _characterId;
private UnityEngine.Camera _mainCamera;
public Image ForeGroundHealthImage;
public Text HealthText;
public static FloatingDamage DamageObject;
void Awake()
{
_mainCamera = UnityEngine.Camera.main;
DamageObject = Resources.Load<FloatingDamage>("UI/FloatingDamage");
}
public void Init(IStateContext context, Guid characterId, VitalType vitalType)
{
_characterId = characterId;
var character = context.Selector.GetCharacter(_characterId);
Debug.Log(gameObject == null);
VitalUiUtils.InitializeVitalBar(vitalType, character, HandleHealthUpdate);
var vital = character.State.Vitals[vitalType];
var healthPercent = vital.CurrentValue / vital.MaxValue;
HealthText.text = $"{Mathf.RoundToInt(vital.CurrentValue)}/{Mathf.RoundToInt(vital.MaxValue)}";
ForeGroundHealthImage.fillAmount = healthPercent;
}
void LateUpdate()
{
transform.LookAt(_mainCamera.transform);
transform.Rotate(0, 180, 0);
}
private void HandleHealthUpdate(float healthPercent, float curValue, float maxValue, float damage)
{
Debug.LogWarning("HEALTH " + HealthText == null);
Debug.LogWarning("HEALTH IMAGE" + ForeGroundHealthImage == null);
//Debug.LogWarning("gameObject " + gameObject == null);
StartCoroutine(VitalUiUtils.UpdateBar(ForeGroundHealthImage, HealthText, healthPercent, curValue, maxValue));
// set the damage transform to follow the player
var instance = Instantiate(DamageObject, transform.parent.position, Quaternion.identity, transform.parent.parent);
// set the damage text to the calculated damage
instance.GetComponent<TextMeshPro>().text = Math.Ceiling(damage).ToString();
}
private void OnDestroy()
{
Debug.LogWarning("DDDDDDDDDDDDDESSSTROY");
}
}
}
OnDestroy does not print anything prior to the above exception it is only being called once I stop the editor
Any help is welcome