【问题标题】:The name '' does not exist in the current context" in the same method名称''在当前上下文中不存在“在同一方法中
【发布时间】:2016-05-17 01:49:35
【问题描述】:

我正在尝试制作游戏,但我无法运行它。原因是因为在这部分:

if (GUI.Button(square,owner)){
    setControl(boardIndex);
}

错误说:

"错误 CS0103: 名称 'square' 在当前不存在 context" "错误 CS0103:名称 'owner' 不存在于 当前上下文" "错误 CS0103:名称 'boardIndex' 不存在 在当前情况下”

但是我之前已经用同样的方法声明过! 这是代码:

using UnityEngine;
using System.Collections;


public class TicTacToeControl : MonoBehaviour {

public SquareState[] board = new SquareState[9]; 
public bool xTurn = true; //empieza X
// Use this for initialization
public void OnGUI(){
    float width = 75;
    float height = 75;

    for (int y= 0; y < 3; y++) {
        for (int x= 0; x < 3; x++) {
            int boardIndex = (y*3)+x;
            Rect square = new Rect(x *width, y*height, width, height); 
            string owner = board[boardIndex]
            == SquareState.XControl ? "X" : board[boardIndex] == SquareState.OControl ? "O" : ""; 
        }
    }

    if (GUI.Button(square,owner)){
        setControl(boardIndex);
    }

}


public void setControl(int boardIndex){  
    if (boardIndex < 0 || boardIndex >= board.Length) {
        return;
        board [boardIndex] = xTurn ? SquareState.XControl : SquareState.OControl;
        xTurn = !xTurn;
    }
}
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    消息已经提到square 未在该范围内定义。但是,它是在您的循环中的另一个(更深的范围)中定义的。因此,您必须将带有GUI 的那部分移动到您的循环中并且它可以工作。

    小侧节点:ownerboardIndex 也应该出现错误。

    有关范围的进一步阅读,请阅读scopes from MSDN

    编辑:您的代码应如下所示:

    for (int y= 0; y < 3; y++) 
    {
        for (int x= 0; x < 3; x++)
        {
            int boardIndex = (y*3)+x;
            Rect square = new Rect(x *width, y*height, width, height); 
            string owner = board[boardIndex] == SquareState.XControl ? "X" : 
                    board[boardIndex] == SquareState.OControl ? "O" : 
                    ""; 
    
            if (GUI.Button(square,owner))
            {
                setControl(boardIndex);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您在循环内定义 square 和 owner 变量,然后在循环外使用它们。

      要么将声明移到循环之外,要么将 setControl 调用移到循环中。

      【讨论】:

        【解决方案3】:

        在循环外创建变量后,你的代码应该是这样的:

                int boardIndex;
                Rect square;
                string owner;
        
                for (int y = 0; y < 3; y++)
                {
                    for (int x = 0; x < 3; x++)
                    {
                        boardIndex = (y * 3) + x;
                        square = new Rect(x * width, y * height, width, height);
                        owner = board[boardIndex] == SquareState.XControl ? "X" : board[boardIndex] == SquareState.OControl ? "O" : "";
                    }
                }
        
                if (GUI.Button(square, owner))
                {
                    setControl(boardIndex);
                }
        

        【讨论】:

          【解决方案4】:

          您在循环体中声明了变量“square”。您不能在循环外使用“正方形”。
          尝试使用此代码。

          public void OnGUI(){
              float width = 75;
              float height = 75;
          
              for (int y= 0; y < 3; y++) {
                  for (int x= 0; x < 3; x++) {
                      int boardIndex = (y*3)+x;
                      Rect square = new Rect(x *width, y*height, width, height); 
                      string owner = board[boardIndex]
                      == SquareState.XControl ? "X" : board[boardIndex] == SquareState.OControl ? "O" : ""; 
                      if (GUI.Button(square,owner))
                          setControl(boardIndex);
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-12-18
            • 2013-10-02
            • 2014-04-22
            • 2017-06-17
            • 2015-09-11
            • 1970-01-01
            • 2015-09-30
            相关资源
            最近更新 更多