help pls

pls help i can’t make the game accept the “public” part of this line of the code “public Tile GetTileAt(int x,int y,int z)” and if i delete the public , i get like 6 other errors witch i tried solve and still couldn’t get it to work
the code is from a tutorial on base building games:/
this is the error if i leave the public on :
Assets\models\World.cs(27,2): error CS0106: The modifier ‘public’ is not valid for this item

and this is the error i get if i don’t leave it : Assets\models\World.cs(17,10): error CS0029: Cannot implicitly convert type ‘Tile[,,]’ to 'Tile[,*]’

Assets\models\World.cs(22,2): error CS0022: Wrong number of indices inside [ ]; expected 2

Assets\models\World.cs(22,18): error CS0029: Cannot implicitly convert type ‘World’ to ‘int’

Assets\models\World.cs(32,11): error CS0022: Wrong number of indices inside [ ]; expected 2

Assets\models\World.cs(27,7): warning CS8321: The local function ‘GetTileAt’ is declared but never used

Assets\models\Tile.cs(7,11): warning CS0414: The field ‘Tile.type’ is assigned but its value is never used

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

public class World {
   
    Tile[,] tiles;
    int width;
    int length;
    int height;
   
    public World(int width = 5 , int length = 5 , int height = 1) {
    this.width = width;
    this.length = length;
    this.height = height;
   
    tiles = new Tile[width,length,height];
   
    for    (int x = 0; x <width; x++) {
        for    (int y = 0; y <length; y++) {
            for    (int z = 0; z <height; z++) {
    tiles [x,y,z] = new Tile[this, x , y , z];
            }
        }
    }
   
    public Tile GetTileAt(int x,int y,int z) {
        if (x > width || x<0 || y > length || y<0 || z > height || z<0) {
            Debug.LogError("Tile ("+x+","+y+","+z+") is out of range.");
            return null;
            }
            return tiles [x,y,z];
        }
    }
}

you need to check where you braces start end.
You did not close the World Function before opening the GetTileAt function.
Thats why public is not valid at line 27

1 Like

You also have all kinds of issues with your “tiles” array.

On line 7 you declare it as a 2 dimensional array.
On line 17 you attempt to assign a 3 dimensional array back into the 2 dimensional array you declared on line 7.
On line 22 you seem to be trying to invoke a constructor for Tile using square brackets [ ] but constructors need to use parentheses: ()
Also on line 22 you are trying to assign a value to a specific element of tiles with 3 indexers, but again, it was only declared as a 2D array.
On line 32 you attempt to access an element of tiles with 3 indexes, which again won’t work as it was declared only as a 2D array.

I think you need to change line 7 to add an extra comma and make it a 3D array.

2 Likes