我正在做的是在我的关卡上,使用Time.deltaTime将计时器从50倒数到0,这非常有用。现在在PlayerControl脚本上,当我拿起一枚硬币时,我希望定时器在3秒内添加。我的代码如下。
//当我拿起PlusOrb对象时,这是PlayerControl。
public class PlayerCTRL : MonoBehaviour
{
public Text timerText;
public TimerCount timerScript;
public Image DamagedOverlay;
public float speed;
public Text CountText;
private int count;
public GameObject TakeDamage;
public Text scoreText;
public TEMPLE_score scoreScript;
public TEMPLE_GlobalSettings globalSettings;
void Start()
{
CountText.text = CoinCounter.ToString();
playerLife = 3;
count = 0;
SetCountText();
playerAS = GetComponent<AudioSource>();
timerScript = GetComponent<TimerCount>();
damaged = false;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("PlusOrb"))
{
Destroy(other.gameObject);
count = count + 1;
SetCountText();
//score
scoreScript.myscore += 2;
scoreText.text = scoreScript.myscore.ToString();
//timer
timerScript.timer + 3f;// this is the problem i am having
PlayerPrefs.SetInt("CoinCount", PlayerPrefs.GetInt("CoinCount") + 1);
}
}
}
//这是计时器脚本。
public class TimerCount : MonoBehaviour
{
public Text TimeText;
public float timer1 = 50;
float SceneTimer = 0;
TEMPLE_GlobalSettings globalSettings;
public Sprite lives0;
public GameObject Gore;
public PlayerCTRL PlayerController;
int ouch;
void Start()
{
timer1 = 50;
}
void Update()
{
this.GetComponent<Text>().text = timer1.ToString("F0");
timer1 -= Time.deltaTime;
print(timer1);
if (timer1 < 1)
{
timer1 = 0;
PlayerController.playerLife = 0;
SceneTimer += Time.deltaTime;
//if (SceneTimer > 2)
//{
//SceneManager.LoadScene("TEMPLE");
//}
}
}
void GameOver()
{
GameObject thisGore = Instantiate(Gore, transform.position, transform.rotation) as GameObject;
thisGore.GetComponent<ParticleSystem>().Play();
GameObject.Find("Lives").GetComponent<Image>().sprite = lives0;
Destroy(gameObject);
}
}
答案 0 :(得分:0)
替换
timerScript.timer + 3;// this is the problem i am having
带
timerScript.timer += 3f;
或
timerScript.timer = timerScript.timer + 3f;
这是一个basic C#的东西。在使用Unity之前,您应该学习C#。那里有很多教程。
修改强>:
使用您问题中的更新脚本,timer
脚本中没有名为TimerCount
的变量。 TimerCount
脚本中的类似变量名称为timer1
。您应该将其重命名为timer
或timerScript.timer1 += 3f;
。
在一天结束时,这意味着您需要学习基本的C#。请不要将此视为侮辱。有必要了解基本的C#,否则你会问更多类似的问题。您正在访问未声明的变量。