首先,這裏的代碼:復讀功能(unity3d/C#)所有的
using UnityEngine;
using System.Collections;
namespace UltimateSurvival
{
public class Radiation : MonoBehaviour
{
public GameObject radiationEffect;
public EntityVitals Vitals { get { return m_Vitals; } }
private EntityVitals m_Vitals;
// Use this for initialization
void Start() {
InvokeRepeating ("OnTriggerEnter", 1.5f, 3.5f);
}
// Update is called once per frame
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
radiationEffect.SetActive(true);
//yield return new WaitForSeconds(5);
var entity = other.GetComponent<EntityEventHandler>();
if(entity)
{
var healthEventData = new HealthEventData(-Random.Range(7.0f, 23.0f));
entity.ChangeHealth.Try(healthEventData);
}
//yield return new WaitForSeconds(5);
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
radiationEffect.SetActive(false);
}
}
}
}
我想要做的是,我想這個腳本來執行OnTriggerEnter每3.5秒。正如你所看到的,我使用InvokeRepeating,但它似乎不起作用。我也試過更改void OnTriggerEnter on IENumerator OntriggerEnter然後返回新的返回WaitForSeconds(5); - 它也沒有工作。我很困惑D:請幫忙!
OnTriggerEnter似乎不是你的代碼上的IENumerator。看看http://answers.unity3d.com/questions/350721/c-yield-waitforseconds.html – iamIcarus
@iamIcarus閱讀我說的腳本後。我也試着改變IENumerator OntriggerEnter上的void OnTriggerEnter,然後返回新的WaitForSeconds(5); - 它也沒有工作。 – papi
Unity消息(開始,更新,OnTriggerEnter,OnDestroy,....)不應該手動調用**。這些消息由Unity本身調用。在你的情況下,由於你不知道什麼時候剛體會進入你的觸發器,所以它**沒有意義。 – Hellium