错误CS1525:意外的符号`insert variable name',期待`。'

时间:2015-10-13 18:18:39

标签: c# unity3d unity5

我在Unity 5中收到此错误消息

  

错误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();
    }
}

2 个答案:

答案 0 :(得分: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());
}

答案 1 :(得分:-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);
    }