Image UI not enabling C# SOLVED
I have been working on this game over screen but when it came to enabling it when dead a problem came there were no errors in fact it seemed to be fine because I could Send a statement to the console when dead and it would work but the enabling of the UI would not. I would love some help with this if you can.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Damage : MonoBehaviour
{
public int maxHealth = 5;
public int currentHealth;
public int DamageCount;
public Image GameOver;
public HealthBar healthBar;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
DamageCount = 0;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
TakeDamage(1);
DamageCount += 1;
}
if (DamageCount == 5)
{
GameOver.enabled = true;
Debug.Log("Enabled");
}
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
}
I found the answer to the problem here is what I changed so basically Instead of GameOver.enabled = true; use GameOver.gameObject.SetActive(true); if the image is a component of a gameobject.
I found the answer to the problem here is what I changed so basically Instead of GameOver.enabled = true; use GameOver.gameObject.SetActive(true); if the image is a component of a gameobject.
Answer by lvskiprof · Feb 16 at 12:10 AM
I have to assume that GameOver is somehow being set in the Unity Editor to an Image object that you have made a child of a canvas, but that is disabled in the Editor settings, correct?
If you enable it in the Editor, just as a test, does it display correctly?
Get it to display correctly there and then disable it and make sure you are referencing the correct Image instance when you code runs.
Yes when I enable it in the editor I can see it but for some reason it doesn't work when enabling it via script