【问题标题】:Var does not exist in the current context [duplicate]Var在当前上下文中不存在[重复]
【发布时间】:2017-10-04 14:24:18
【问题描述】:

我正在用 Unity 做“PlayerMoveKeyBoard”,我使用了这个代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovingScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
        double x;
        double y;
        double z;
        double speed;
    }

    // Update is called once per frame
    void Update () {
        speed = speed * 0.95;
        if (Input.GetKeyDown("Up")) {
            speed += 2;
            transform.Translate (speed, 0, 0);
        }
        if (Input.GetKeyDown("Down")) {
            speed += 2;
            transform.Translate (-speed, 0, 0);
        }
        if (Input.GetKeyDown("Right")) {
            speed += 2;
            transform.Translate (0, 0, speed);
        }
        if (Input.GetKeyDown("Left")) {
            speed += 2;
            transform.Translate (0, 0, -speed);
        }
    }
}

但它说:

(17, 11) 当前上下文中不存在名称“speed”

【问题讨论】:

  • 从代码给出的权利 - 你还没有声明速度 - 变量只存在于“开始”然后死
  • 等等....它需要是浮动的,而不是双倍的
  • floatdouble 对这种错误没有影响
  • Variable scope,学习如何编码的早期课程之一......

标签: c# unity3d


【解决方案1】:

你已经在方法Start的范围内本地声明了变量:

void Start () {
    double x;
    double y;
    double z;
    double speed;
}

在此方法之外无法访问这些变量。您需要在类级别声明它们:

public class MovingScript : MonoBehaviour {

    double x;
    double y;
    double z;
    double speed;

    // Update is called once per frame
    void Update () {
        speed = speed * 0.95;
        if (Input.GetKeyDown("Up")) {
            speed += 2;
            transform.Translate (speed, 0, 0);
        }
        if (Input.GetKeyDown("Down")) {
            speed += 2;
            transform.Translate (-speed, 0, 0);
        }
        if (Input.GetKeyDown("Right")) {
            speed += 2;
            transform.Translate (0, 0, speed);
        }
        if (Input.GetKeyDown("Left")) {
            speed += 2;
            transform.Translate (0, 0, -speed);
        }
    }
}

请阅读Variables and Method Scope in this article

【讨论】:

    【解决方案2】:

    您在称为局部变量的方法中声明的所有变量,并且只能在该方法中访问。这里 speed 没有在 Update 方法中声明,所以你不能访问它。一旦你离开 Start 方法的范围,它就会在 Start 方法中声明它消失。为了在 Update 方法中访问它,您需要在 Update 方法中声明它。

    像这样:

     void Update()
        {
            float speed = 0.0f;
            speed = speed * 0.95;
            if (Input.GetKeyDown("Up"))
            {
                speed += 2;
                transform.Translate(speed, 0, 0);
            }
            if (Input.GetKeyDown("Down"))
            {
                speed += 2;
                transform.Translate(-speed, 0, 0);
            }
            if (Input.GetKeyDown("Right"))
            {
                speed += 2;
                transform.Translate(0, 0, speed);
            }
            if (Input.GetKeyDown("Left"))
            {
                speed += 2;
                transform.Translate(0, 0, -speed);
            }
        }
    }
    

    【讨论】:

    • 好的,我试试,希望成功
    • 不,它不起作用,因为 transform.Translate() 需要浮点数,而不是双精度(也许我会用浮点数替换双精度并添加“f”
    【解决方案3】:

    您需要在开始之外的类本身中声明速度。

        public class MovingScript : MonoBehaviour {
    
        double speed;
    
        // Use this for initialization
        void Start () {
            double x;
            double y;
            double z;
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-07
      • 2014-02-28
      • 1970-01-01
      • 2023-03-30
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多