简体   繁体   English

隐藏健康吧直到敌人靠近C#

[英]Hiding Health Bar Until Enemy is nearby C#

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(). 我也很熟悉使其成为isTrigger事件以及OnTriggerEnter()的功能。

What my problem seem to be is reading the image from my Health Point Script(HPsc) into my EnemyDetection script. 我的问题似乎是从健康点脚本(HPsc)将图像读取到EnemyDetection脚本中。

In my HPsc I've declared and assigned my HP images (red and green) as public static Image HP_Green, HP_Red. 在我的HPsc中,我已经将我的HP映像(红色和绿色)声明并分配为公共静态映像HP_Green,HP_Red。 So in my EnemyDetection script I reference these HPsc.HP_Green.SetActive(True) and HPsc.HP_Red.SetActive(true) 因此,在我的EnemyDetection脚本中,我引用了这些HPsc.HP_Green.SetActive(True)和HPsc.HP_Red.SetActive(true)

However, the problem seem to be that SetActive is for text UI and not for Image? 但是,问题似乎在于SetActive是用于文本UI而非图像? Could FadeIn() and FadeOut() actually be an alternative here? FadeIn()和FadeOut()在这里实际上可以替代吗?

HealthBar Class: HealthBar类:

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: 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); *****解决方案#1 ***** HP_Bar_Green.gameObject.SetActive(true); – Fredrik Widerberg – Fredrik Widerberg

Just needed to string the gameObject in there! 只需在其中将gameObject串起来即可! Thanks all! 谢谢大家!

****** ISSUE #2 ****** Ok, now my next issue with this code is getting BOTH images into the correct variables. ******问题#2 ******好吧,现在我的下一个代码问题是将两个图像都放入正确的变量中。

HP_Bar_Green = transform.GetChild(0).gameObject.GetComponent(); HP_Bar_Green = transform.GetChild(0).gameObject.GetComponent(); HP_Bar_Red = transform.GetChild(1).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. .GetChild()看上去很吸引人,但是却引起了很多问题。 The compiler allows the game to start and play, but over 5 minutes I stacked up 20,000 identical errors in the compiler. 编译器允许游戏开始运行,但是在5分钟内,我在编译器中堆积了20,000个相同的错误。

***** Issue is RESOLVED ***** *****问题已解决*****

Hallelujah! 哈利路亚!

I moved HP_Bar_Red into my EnemyDetection script. 我将HP_Bar_Red移到了EnemyDetection脚本中。 Made it a public variable and manually inserted the object inside Unity inspector. 将其设为公共变量,然后将对象手动插入Unity inspector中。 (As a few people have been recommending here. Thanks to you all! Stay blessed!) (因为有人在这里推荐。谢谢大家!祝你幸福!)

So, you are trying to do SetActive() on a Image component. 因此,您正在尝试对Image组件执行SetActive() However, SetActive() is a part of GameObject to activate/deactivate the GameObject in your hierachy. 但是, SetActive()GameObject的一部分,用于在您的GameObject中激活/停用GameObject

So you need to get the GameObject first, and call SetActive() on it 因此,您需要先获取GameObject ,然后对其调用SetActive()

myImage.gameObject.SetActive(true);

If you want to enable/disable just the Image -component you could do 如果只想启用/禁用Image ,则可以执行

myImage.enabled = true;

You need to set the GameObject on the Image component to be active or inactive using 您需要使用以下命令将Image组件上的GameObject设置为活动或不活动

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. 您可能会遇到使HpSc.HP_Bar_Green处于静态的问题,因此,如果有多个敌人,您可能希望拥有一个获取HpSc组件并针对该特定组件禁用它的类。

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>();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM