我知道如何创建一个对撞机来检测被标记为“敌人”的游戏对象。我还熟悉如何使它成为isTrigger事件以及OnTriggerEnter()的功能。
我的问题似乎是将图像从我的健康点脚本(HPsc)读取到我的EnemyDetection脚本中。
在我的HPsc中,我声明并将我的HP映像(红色和绿色)指定为公共静态图像HP_Green,HP_Red。因此,在我的EnemyDetection脚本中,我引用了这些HPsc.HP_Green.SetActive(真)和HPsc.HP_Red.SetActive(真)
然而,问题似乎是SetActive是用于文本UI,而不是针对图像?在这里,FadeIn()和FadeOut()实际上是一种选择吗?
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:
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.
}
}
}*解决方案问题#1 *HP_Bar_Green.gameObject.SetActive(真);- Fredrik Widerberg
只需要把gameObject串在里面就行了!谢谢大家!
*问题#2 *,现在我在这段代码中的下一个问题是将这两个图像都放入正确的变量中。
transform.GetChild(0).gameObject.GetComponent();HP_Bar_Red = transform.GetChild(1).gameObject.GetComponent();
我试图确保每个变量只能包含它应该包含的内容。.GetChild()看起来很好,但是它引起了很多问题。编译器允许游戏开始和播放,但在5分钟内,我在编译器中堆放了20,000个相同的错误。
*问题已解决*
哈利路亚!
我将HP_Bar_Red移到我的EnemyDetection脚本中。使其成为一个公共变量,并手动将该对象插入到中。(正如一些人在这里所建议的那样。感谢你们所有人!祝福吧!)
发布于 2018-08-23 19:26:35
因此,您正在尝试对一个SetActive()组件执行Image操作。但是,SetActive()是GameObject的一部分,用于激活/禁用您的等级中的GameObject。
所以你需要先得到GameObject,然后调用SetActive()
myImage.gameObject.SetActive(true);如果您只想启用/禁用Image-component,则可以这样做
myImage.enabled = true;发布于 2018-08-23 19:09:45
需要使用以下方法将图像组件上的GameObject设置为活动或非活动
GameObject.SetActive(boolean)或者你也可以这样做
HpSc.HP_Bar_Green.enabled = true/false;类:
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.
}
}
}如果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;
}
}
}非静态变量:
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>();
}
}https://stackoverflow.com/questions/51991551
复制相似问题