C# 统一使用心脏的卫生系统
我得到了这个脚本,并试图添加一种让玩家失去信心的方式。我不确定我是否遗漏了什么。我试过剧本,但什么也没发生。这也适用于2d平板电脑,最重要的是,如果玩家摔倒,我只希望他失去一颗心。请帮忙C# 统一使用心脏的卫生系统,c#,unity3d,C#,Unity3d,我得到了这个脚本,并试图添加一种让玩家失去信心的方式。我不确定我是否遗漏了什么。我试过剧本,但什么也没发生。这也适用于2d平板电脑,最重要的是,如果玩家摔倒,我只希望他失去一颗心。请帮忙 using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; // Attach to an empty GameObject // To initialize sc
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
// Attach to an empty GameObject
// To initialize script on a new scene, add updateHealthUI() in the Awake
or Start Method of your player
// Then just use this script in your OnCollision method using
thisScript.Health --
public class PlayerHealth : MonoBehaviour // MonoBehaviour
{
// Insert your 3 hearts images in the Unity Editor
public Image h1, h2, h3;
// Create an array because we're lazy
public Image[] images;
// Gameover
[SerializeField] private Image gameOver;
// A private variable to keep between scenes
int health = 3;
// Now we define Get / Set methods for health
// In case we Set health to a different value we want to update UI
public int Health { get { return health; } set { if (health != Health)
health = Health; updateHealthUI(); } }
public void Awake()
{
DontDestroyOnLoad(this.gameObject);
images = new Image[] { h1, h2, h3 };
if (transform.position.y == -80)
{
health--;
}
}
private void updateHealthUI()
{
for (int i = 0; i < images.Length; i++)
{
// Hide all images superior to the newHealth
if (i >= health)
images[i].enabled = false;
else
images[i].enabled = true;
}
// Game Over
if (health == 0)
{
SceneManager.LoadScene(2);
}
}
void Update()
{
if (health == 3)
{
images = new Image[] { h1, h2, h3 };
}
else if (health == 2)
{
images = new Image[] { h1, h2 };
}
else if (health == 1)
{
images = new Image[] { h1 };
}
}
}
使用UnityEngine;
使用系统集合;
使用UnityEngine.UI;
使用UnityEngine.SceneManagement;
//附加到一个空的游戏对象
//要在新场景中初始化脚本,请在“唤醒”对话框中添加updateHealthUI()
或者玩家的启动方法
//然后在你的迎面碰撞方法中使用这个脚本
这个脚本。健康--
公共类玩家健康:单行为//单行为
{
//在Unity编辑器中插入3个心脏图像
公众形象h1、h2、h3;
//创建一个数组,因为我们很懒
公众形象[]形象;
//游戏结束
[SerializeField]私有映像gameOver;
//在场景之间保留的私有变量
int-health=3;
//现在我们为健康定义Get/Set方法
//如果我们将“运行状况”设置为不同的值,则需要更新UI
public int Health{get{return Health;}set{if(Health!=Health)
健康=健康;更新健康();}
公共图书馆
{
DontDestroyOnLoad(this.gameObject);
图像=新图像[]{h1,h2,h3};
if(transform.position.y==-80)
{
健康--;
}
}
私有void updateHealthUI()
{
对于(int i=0;i=健康)
图像[i]。已启用=错误;
其他的
图像[i].enabled=true;
}
//游戏结束
如果(运行状况==0)
{
场景管理器。加载场景(2);
}
}
无效更新()
{
如果(健康==3)
{
图像=新图像[]{h1,h2,h3};
}
否则如果(运行状况==2)
{
图像=新图像[]{h1,h2};
}
否则如果(运行状况==1)
{
图像=新图像[]{h1};
}
}
}
您正试图降低系统的运行状况。问题在于,在脚本实例的生命周期中,Awake
只被调用一次。因此,尝试将if语句放入Update
方法,而不是Awake
:
if (transform.position.y == -80)
{
health--;
}
@丹尼尔很高兴你的问题解决了。如果你发现这个答案是有用的,你可以考虑选择它作为问题的答案,以便其他可能面对类似问题的人会看到这一点。