0
votes

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!)

2
I believe to hide and unhide gui components you have to perform it on their canvas renderers.jiveturkey
You need to show the code you are using, the explanation are not really helping.Everts
Hey, thanks for the response! I'm not sure how to properly add the images and/or code... Perhaps I'll have to ask stacked that too? smh lol Anyway, reallly quick, how do I insert the C# so you can see it clean?Tim Duval
What is the definition of HP_Bar_Green? Im guessing you need to do HP_Bar_Green.gameObject.SetActive(true);Fredrik Widerberg
@FredrikWiderberg I'll give it a try!Tim Duval

2 Answers

1
votes

So, you are trying to do SetActive() on a Image component. However, SetActive() is a part of GameObject to activate/deactivate the GameObject in your hierachy.

So you need to get the GameObject first, and call SetActive() on it

myImage.gameObject.SetActive(true);

If you want to enable/disable just the Image-component you could do

myImage.enabled = true;
0
votes

You need to set the GameObject on the Image component to be active or inactive using

GameObject.SetActive(boolean)

Or Alternatively you could do this

HpSc.HP_Bar_Green.enabled = true/false;

Class:

public class EnemyDetection
{
    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Enemy1")
        {
            HpSc.HP_Bar_Green.GameObject.SetActive(true);
            //or
            HpSc.HP_Bar_Green.enabled = true;
            // This is DetectEnemy trying to grab the image from the other script.
        }
    }
}

You might run into issue with having the HpSc.HP_Bar_Green being static so if there are multiple enemies you might want to have a class that gets the HpSc component and disables it for that specific one.

public class EnemyDetection
{
    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Enemy1")
        {
            HpSc healthBar = other.GetComponent<HpSc>();

            healthBar.GameObject.SetActive(true);
            //or
            healthBar.enabled = true;
        }
    }
}

Non Static variables:

public class HPsc
{
    public Image HP_Bar_Green;
    public Image HP_Bar_Red; // Allows me to initialize the images in Unity

    void Start()
    {
        HP_Bar_Green = this.GetComponent<Image>();
        HP_Bar_Red = this.GetComponent<Image>();
    }
}