0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class World {
    Tile[,] tiles;
    int width;
    int height;

    public World (int width = 200, int height = 200) {
        this.width = width;
        this.height = height;

        tiles = new Tile[width, height];

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                tiles[x, y] = new Tile[this, x, y];
            }
        }
    }

    public Tile GetTileAt(int x, int y) {
        if (x > width || x < 0 || y > height || y < 0) {
            Debug.LogError ("Tile (" + x + ", " + y + ") is out of range");
            return null;
        }
        return tiles [x, y];
    }

}

anyone see anything wrong with this code because on line 18 of my code i get this error "cannot implicitly convert type 'World' to 'int' and i don't know what is going wrong.

\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

Is it safe to presume this line is the culprit?

tiles[x, y] = new Tile[this, x, y];

(It never hurts to annotate your code with a comment like \\ This line generates the following error... to ensure it's clear for people trying to help)

It looks like you're trying to call a constructor for the Tile class, passing this world as the first argument. But instead of using round parentheses ( ) for a function call, you've used the square brackets [ ] of an array index or size declaration.

So, this says "Make a new 3-dimensional array of tiles, where the first dimension's length is the integer this" ...but this isn't an integer, this is a World!

Thus the compiler throws up its hands and says "fix this - you've asked me to do something nonsensical here" ;)

\$\endgroup\$
2
  • \$\begingroup\$ yeah thats the line thats throwing me a error, so are you saying that i should change it from [] to ()? \$\endgroup\$
    – Shaow Wolf
    Commented Mar 24, 2017 at 17:23
  • \$\begingroup\$ Yep! new SomeType[...] means "I'm creating a new array with the following dimensions" while new SomeType(...) means I'm creating a new object/struct with the following constructor arguments" \$\endgroup\$
    – DMGregory
    Commented Mar 24, 2017 at 17:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .