2015-10-13 76 views
-2

我得到了統一5此錯誤消息錯誤CS1525:意外的符號`插入變量名稱',期望`。'

錯誤CS1525:意外的符號insert variable name', expecting「。

using UnityEngine; 
using System.Collections; 

public class jumpControll : MonoBehaviour 
{ 

    public bool jump; 
    public float jumpHeight; 



    // Use this for initialization 

    public IEnumerator jumpUp (float jumpHeight) 
    { 
     jumpHeight = 3.0f; 
     transform.position = new Vector3(transform.position.x, 
      transform.position.y + jumpHeight, transform.position.z); 
    } 

    public IEnumerator jumpDown (float jumpHeight) 
    { 
     jumpHeight = 3.0f; 
     transform.position = new Vector3(transform.position.x, 
      transform.position.y - jumpHeight, transform.position.z); 
    } 

    public IEnumerator jumpTest() 
    { 
     jumpUp(float jumpHeight); 
     yield return new WaitForSeconds(1); 
     jumpDown(float jumpHeight); 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     jump = Input.GetKey(KeyCode.Space); 
     if (jump == true) 
      jumpTest(); 
    } 
} 
+1

錯誤發生在哪裏?什麼行號/字符位置?它發生在構建時間還是運行時間? – Necoras

回答

-1
public IEnumerator jumpUp(float jumpHeight) 
    { 
     transform.position = new Vector3(transform.position.x, transform.position.y + jumpHeight, transform.position.z); 
    } 

    public IEnumerator jumpDown(float jumpHeight) 
    { 
     transform.position = new Vector3(transform.position.x, transform.position.y - jumpHeight, transform.position.z); 
    } 

    public IEnumerator jumpTest() 
    { 
     jumpUp(3.0f); 
     yield return new WaitForSeconds(1); 
     jumpDown(3.0f); 
    } 
0

有幾件事情這是不會允許你的代碼進行編譯。

首先,你指定你的jumpUpjumpDown方法將返回一個IEnumerator但是,你不返回一個。如果您不需要在jumpUpjumpDown中等待一段時間,則它們應該具有void而不是IEnumerator的返回類型。

public void jumpUp (float jumpHeight) 
{ 
    jumpHeight = 3.0f; 
    transform.position = new Vector3(transform.position.x,    
            transform.position.y + jumpHeight, 
            transform.position.z); 
} 

public void jumpDown (float jumpHeight) 
{ 
    jumpHeight = 3.0f; 
    transform.position = new Vector3(transform.position.x, 
            transform.position.y - jumpHeight, 
            transform.position.z); 
} 

其次,在你的jumpTest方法,嘗試調用jumpUpjumpDown傳遞您的jumpHeight變量。您不需要在要傳遞的變量前面使用float關鍵字,因爲您沒有創建新變量,而是使用了已存在的變量。

public IEnumerator jumpTest() 
{ 
    jumpUp(jumpHeight); 
    yield return new WaitForSeconds(1); 
    jumpDown(jumpHeight); 
} 

最後,爲了一個IEnumerator方法才能正常工作,您需要使用StartCoroutine()調用它。舉例來說,在您的更新方法中:

void Update() 
{ 
    jump = Input.GetKey(KeyCode.Space); 
    if (jump == true) 
     StartCoroutine(jumpTest()); 
} 
相關問題