亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解決 IEnumerator(Unity, C#) 的錯誤?

如何解決 IEnumerator(Unity, C#) 的錯誤?

C#
largeQ 2023-09-24 16:32:35
我需要讓我的角色墻運行,但我有代碼問題IEnumerator這是用Unity 4.5.xC# 編寫的代碼using UnityEngine;using System.Collections;public class Moving : MonoBehaviour {public float speed = 6.0F;public float jumpSpeed = 8.0F; public float gravity = 20.0F;public float runTime = 1.0f;private Vector3 moveDirection = Vector3.zero;private bool isWallL = false;private bool isWallR = false;private RaycastHit hitL;private RaycastHit hitR;private int jumpCount = 1;IEnumerator afterRun() {    yield return new WaitForSeconds (runTime);    isWallL = false;    isWallR = false;    gravity = 20;}void Update() {    CharacterController controller = GetComponent<CharacterController>();    if (controller.isGrounded) {        jumpCount = 0;        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));        moveDirection = transform.TransformDirection(moveDirection);        moveDirection *= speed;        if (Input.GetButton("Jump"))            moveDirection.y = jumpSpeed;    }    if (Input.GetKeyDown (KeyCode.Space) && !controller.isGrounded && jumpCount <= 1) {        if (Physics.Raycast (transform.position, -transform.right, out hitL, 1)){            if (hitL.transform.tag == "Wall"){                isWallL = true;                isWallR = false;                jumpCount = 1;                gravity = 0;                StartCoroutine (afterRun);            }        }        if (Physics.Raycast (transform.position, transform.right, out hitR, 1)){            if (hitR.transform.tag == "Wall"){                isWallL = false;                isWallR = true;                jumpCount = 1;                gravity = 0;                StartCoroutine (afterRun);            }        }    }    moveDirection.y -= gravity * Time.deltaTime;    controller.Move(moveDirection * Time.deltaTime);    }}預計沒有錯誤,但我有兩個:錯誤CS1502:UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)'的最佳重載方法匹配有一些無效參數”和“錯誤CS1503:參數#1'無法將方法組'表達式轉換為類型System.Collections.IEnumerator' 。
查看完整描述

3 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

代碼中的 afterRun 是一個函數,但您在調用它時不使用括號。所以:


StartCoroutine (afterRun());

例如:


namespace someNamespace

{?

? ? public class SomeClass

? ? {

? ? ? ? IEnumerator afterRun()

? ? ? ? {

? ? ? ? ? ? yield return new WaitForSeconds(3);? ? ? ? ? ??

? ? ? ? }


? ? ? ? public void Test(IEnumerator enumerator)

? ? ? ? {

? ? ? ? ? ? while(enumerator.MoveNext())

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //do some work

? ? ? ? ? ? }

? ? ? ? }


? ? ? ? public void YoureCode()

? ? ? ? {

? ? ? ? ? ? Test(afterRun());

? ? ? ? }

? ? }


? ? public class WaitForSeconds

? ? {

? ? ? ? public WaitForSeconds(int a)

? ? ? ? {? ? ? ? ? ??

? ? ? ? }

? ? }

}


查看完整回答
反對 回復 2023-09-24
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

為什么不這樣:


private IEnumerator coroutine;

然后設置并調用它:


coroutine = afterRun();

StartCoroutine(coroutine);


查看完整回答
反對 回復 2023-09-24
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

根據 Unity協程的文檔,看來協程函數必須被調用為StartCoroutine ("afterRun");



查看完整回答
反對 回復 2023-09-24
  • 3 回答
  • 0 關注
  • 321 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號