简体   繁体   English

如何在 c# function 中使用 2D GameObject 数组作为参数(2D 统一游戏)

[英]How to use 2D GameObject array as a parameter in a c# function ( 2D unity game)

I'm a newbie at unity/c#, and i'm coding a 2D chess game using unity, and i'm having some trouble making functions.我是 unity/c# 的新手,我正在使用 unity 编写 2D 国际象棋游戏,但在制作函数时遇到了一些麻烦。 I'm trying to setup a function that renders pieces on the board, but i can't seem to make it work.我正在尝试设置一个 function 来渲染板上的碎片,但我似乎无法让它工作。 I want to use 2D array as a third parameter in this function but I keep getting this error: error CS1503: Argument 3: cannot convert from 'UnityEngine.GameObject' to 'UnityEngine.GameObject[,]' .我想在此 function 中使用 2D 数组作为第三个参数,但我不断收到此错误:错误 CS1503: Argument 3: cannot convert from 'UnityEngine.GameObject' to 'UnityEngine.GameObject[,]' This is my board class:这是我的板子 class:

void Start()
{
    boardScript = GameObject.FindGameObjectWithTag("BoardScript").GetComponent<Board>();
    spawn = GameObject.FindGameObjectWithTag("GameController").GetComponent<SpawnPieces>();

    boardScript.createBoard();
}

This is one of my piece classes:这是我的一门课:

public void spawnQueen(int x, int y, GameObject[,] board)
{
    if (x == 3 && y == 0)
    {
        board[x, y] = Instantiate(whiteQueen, new Vector3(x * 4.49f, y * 4.49f, -1), Quaternion.identity);
    }
    else if (x == 3 && y == 7)
    {
        board[x, y] = Instantiate(blackQueen, new Vector3(x * 4.49f, y * 4.49f, -1), Quaternion.identity);
    }
}

And this is the class that will render the pieces:这是 class 将渲染这些片段:

public void spawnPiece()
{
    boardScript = GameObject.FindGameObjectWithTag("BoardScript").GetComponent<Board>();
    GameObject[,] boardArray = boardScript.board;

    rookScript = GameObject.FindGameObjectWithTag("RookScript").GetComponent<RookScript>();
    pawnScript = GameObject.FindGameObjectWithTag("PawnScript").GetComponent<PawnScript>();
    kingScript = GameObject.FindGameObjectWithTag("KingScript").GetComponent<KingScript>();
    bishopScript = GameObject.FindGameObjectWithTag("BishopScript").GetComponent<BishopScript>();
    kinghtScript = GameObject.FindGameObjectWithTag("knightScript").GetComponent<knightScript>();
    queenScript = GameObject.FindGameObjectWithTag("QueenScript").GetComponent<QueenScript>();

    for (int x = 0; x < 8; x++)
    {
        for (int y = 0; y < 8; y++)
        {
            rookScript.spawnRook(x, y, boardArray[x, y]);
            pawnScript.spawnPawn(x, y, boardArray[x, y]);
            knightScript.spawnKnight(x, y, boardArray[x, y]);
            kingScript.spawnKing(x, y, boardArray[x, y]);
            queenScript.spawnQueen(x, y, boardArray[x, y]);
            bishopScript.spawnBishop(x, y, boardArray[x, y]);
        }
    }
}

As I have mentionned I am new to C#, any help would be appreciated.正如我所提到的,我是 C# 的新手,我们将不胜感激。

With the way you call your method you are passing an element of the boardArray instead of the whole array.通过调用方法的方式,您传递的是 boardArray 的一个element ,而不是整个数组。 Try calling the method like this:尝试调用这样的方法:

bishopScript.spawnBishop(x, y, boardArray);

Notice the third argument is boardArray as opposed to boardArray[x, y] .注意第三个参数是boardArray而不是boardArray[x, y]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM