【发布时间】: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;
}
}
}
【问题讨论】: