返回游戏对象的数组列表和网格问题

本文关键字:网格 问题 列表 数组 游戏 对象 返回 | 更新日期: 2023-09-27 18:32:00

我试图生成一个网格Unity3d并将所有瓷砖存储在一个数组中。我也想在其他函数中使用创建的数组。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class createGridClean : MonoBehaviour {
    public GameObject tilePrefab = null;
    public int numberOfTiles = 10;
    public int tilesPerColumn = 4;
    public float distanceBetweenTiles = 2.0f;
    public Material newmat = null;
    // Use this for initialization
    void Start () {
        CreateTiles ();
    }
    // Update is called once per frame
    void Update () {
    }
    public ArrayList[] CreateTiles(){
        GameObject[] allTiles = new GameObject[numberOfTiles*tilesPerColumn];
        float xOffset = 0.0f;
        float zOffset = 0.0f;
        for (int createdTiles = 0; createdTiles <= numberOfTiles; createdTiles++) {
            xOffset += distanceBetweenTiles;
            if ( createdTiles % tilesPerColumn == 0){
                zOffset += distanceBetweenTiles;
                xOffset = 0.0f;
            }
            allTiles[createdTiles] = Instantiate (tilePrefab, new Vector3 (0f + xOffset, 0f, 0f + zOffset), Quaternion.identity) as GameObject;
        }
        return allTiles;
    }
}

但这是输出:

Assets/my/Script/createGridClean.cs(34,17): error CS0029: Cannot implicitly convert type `UnityEngine.GameObject[]' to `System.Collections.ArrayList[]'

我什至对网格有问题,每次都会生成一个额外的和不需要的额外立方体!

(这是我用 C# 翻译的原始代码)

function CreateTiles()
{
    var xOffset: float = 0.0;
    var zOffset: float = 0.0;
    for(var tilesCreated: int = 0; tilesCreated < numberOfTiles; tilesCreated += 1)
    {
        xOffset += distanceBetweenTiles;
        if(tilesCreated % tilesPerRow == 0)
        {
            zOffset += distanceBetweenTiles;
            xOffset = 0;
        }
        Instantiate(tilePrefab, Vector3(transform.position.x + xOffset, transform.position.y, transform.position.z + zOffset), transform.rotation);
    }
}

任何帮助都会很棒!

谢谢马可

返回游戏对象的数组列表和网格问题

尝试更改

public ArrayList[] CreateTiles()
{
    GameObject[] allTiles = new GameObject[numberOfTiles*tilesPerColumn];
...
    return allTiles;
}

自:

public GameObject[] CreateTiles()
{
    GameObject[] allTiles = new GameObject[numberOfTiles*tilesPerColumn];
...
    return allTiles;
}

您收到的错误消息:Cannot implicitly convert type X to Y是指您将GameObject[]隐式转换为ArrayList[]的事实。 您必须显式转换为ArrayList[](例如 return (ArrayList[]) allTiles,虽然我不确定这就是你所追求的),或者更改您的方法以返回GameObject[].

您的问题是所有其他问题的重复:https://stackoverflow.com/search?q=cannot+implicitly+convert