I know how to create a collider to detect a game object tagged as "Enemy". I'm also familiar with making it an isTrigger event as well as the functionality of OnTriggerEnter().
What my problem seem to be is reading the image from my Health Point Script(HPsc) into my EnemyDetection script.
In my HPsc I've declared and assigned my HP images (red and green) as public static Image HP_Green, HP_Red. So in my EnemyDetection script I reference these HPsc.HP_Green.SetActive(True) and HPsc.HP_Red.SetActive(true)
However, the problem seem to be that SetActive is for text UI and not for Image? Could FadeIn() and FadeOut() actually be an alternative here?
HealthBar Class:
public class HPsc
{
public static Image HP_Bar_Green;
public static Image HP_Bar_Red; // Allows me to initialize the images in Unity
void Start()
{
HP_Bar_Green = GetComponent<Image>();
HP_Bar_Red = GetComponent<Image>(); //How I grab the images upon startup
}
}
EnemyDetection:
public class EnemyDetection
{
void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy1")
{
HpSc.HP_Bar_Green.SetActive(true);
// This is DetectEnemy trying to grab the image from the other script.
}
}
}
***** SOLUTION ISSUE #1 ***** HP_Bar_Green.gameObject.SetActive(true); – Fredrik Widerberg
Just needed to string the gameObject in there! Thanks all!
****** ISSUE #2 ****** Ok, now my next issue with this code is getting BOTH images into the correct variables.
HP_Bar_Green = transform.GetChild(0).gameObject.GetComponent(); HP_Bar_Red = transform.GetChild(1).gameObject.GetComponent();
I'm trying to insure that each variable can only contain what its supposed to. .GetChild() looks promosing but it is causing a lot of issues. The compiler allows the game to start and play, but over 5 minutes I stacked up 20,000 identical errors in the compiler.
***** Issue is RESOLVED *****
Hallelujah!
I moved HP_Bar_Red into my EnemyDetection script. Made it a public variable and manually inserted the object inside Unity inspector. (As a few people have been recommending here. Thanks to you all! Stay blessed!)