0

I've got this little chunk here, but I was wondering if I could use GetTile() using a specific variable within the array.

EDIT: my entire script is:

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

public class walltilemap : MonoBehaviour
{
    public TileBase Ground_tiles_3;
    public Vector2Int size;
    void Start(){
        Vector3Int[] positions = new Vector3Int[size.x * size.y];
        TileBase[] tileArray = new TileBase[positions.Length];

        for (int index = 0; index < positions.Length; index++){
            positions[index] = new Vector3Int(index % size.x, index / size.y, 0);
            bool iswalltile() {
                if (Tilemap.GetTile(positions[index])){

                }
                return true;
            }
            
            tileArray[index] = iswalltile() == true ? Ground_tiles_3 : null;
        }
        Tilemap WallTileMap = GetComponent<Tilemap>();
        WallTileMap.SetTiles(positions, tileArray);
    }
}
  • 2
    _"wondering if i could use GetTile using a specific variable within the array"_ -- huh? Isn't that what your code does now? Please fix your question so that it's not unclear. Be sure to explain precisely what the code you have now is doing, how that's different from what you want, and what _specifically_ you need help with. – Peter Duniho Oct 16 '20 at 03:46
  • oh, yeah sorry. my problem is is that it doesnt actually work, so im not sure what the problem is. just adding the 0 gives an error: ```Assets\walltilemap.cs(17,21): error CS0120: An object reference is required for the non-static field, method, or property 'Tilemap.GetTile(Vector3Int)'``` – nightwitchsara Oct 16 '20 at 03:47
  • unfortunately, that doesn't work either – nightwitchsara Oct 16 '20 at 03:49
  • The way you've posted the code, the `iswalltile()` method is a **local function**, and so `positions` would be valid there just as it's valid in the loop in which the local function is declared. Is that really how your code looks? If not, please fix your question so that the code is actually _exactly_ what you're actually having trouble with. _(As an aside: .NET/C# code typically eschews the K&R bracing style...you will find most people have an easier time reading your code examples if you use aligned braces instead of K&R)._ – Peter Duniho Oct 16 '20 at 03:50
  • how is Tilemap null? how else would i go about trying to use ```GetTile``` is it possible I just got the syntax wrong? – nightwitchsara Oct 16 '20 at 03:52
  • It's not null. @John picked the wrong duplicate. I fixed the dupes list. You seem to be trying to call the `GetTile()` method using the _name_ of the `Tilemap` class. This is incorrect. You need to use a _reference_ to an actual _instance_ of that class. See the duplicates for details. Also see the Unity3d docs for details on how to use `Tilemap`, if you don't understand what is meant by _reference_ and _instance_. There should be examples that show how to create and use one. https://docs.unity3d.com/Manual/class-Tilemap.html – Peter Duniho Oct 16 '20 at 03:56
  • ah, I think I see. so would I reference the tilemap itself in question? – nightwitchsara Oct 16 '20 at 03:58
  • _"would I reference the tilemap itself in question?"_ -- yes. You ought to have a variable somewhere that references the `Tilemap` instance. You need to use the name of that variable (or possible property), instead of the name `Tilemap`. – Peter Duniho Oct 16 '20 at 03:59
  • Does this answer your question? [CS0120: An object reference is required for the nonstatic field, method, or property 'foo'](https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop) – Peter Duniho Oct 16 '20 at 04:00
  • Does this answer your question? [Error: “an object reference is required for the non-static field, method or property…” \[duplicate\]](https://stackoverflow.com/questions/2505181/error-an-object-reference-is-required-for-the-non-static-field-method-or-prop) – Peter Duniho Oct 16 '20 at 04:00
  • so my tilemap in this case is named WallTileMap. how would I go about referencing that? Because just replacing Tilemap with WallTileMap didnt work – nightwitchsara Oct 16 '20 at 04:01
  • Does this answer your question? [C# error: “An object reference is required for the non-static field, method, or property”](https://stackoverflow.com/questions/10264308/c-sharp-error-an-object-reference-is-required-for-the-non-static-field-method) – Peter Duniho Oct 16 '20 at 04:01
  • ah I believe the issue was fixed by declaring the Tilemap before the GetTile request and using the name of it! Thank you for your assistance :) – nightwitchsara Oct 16 '20 at 04:04
  • _"my tilemap in this case is named WallTileMap"_ -- I assume by "named" you mean that you have created the object in the script somewhere. That name will exist somewhere, but not necessarily in the class where your code is. Unfortunately, you haven't provided enough code to know where it _is_ declared. What you really need is to review how scoping of names in C# works; any data is always stored in a field somewhere, and accessed through fields, properties, methods, etc. Data (which in some cases is a reference) must be stored in a public member for other classes to access it, or passed ... – Peter Duniho Oct 16 '20 at 04:06
  • ... to other classes (e.g. by setting a field or property, or calling a method, in the other class) for data to be shared. This is all really basic C#/OOP stuff, but if you're not far enough along in your programming education it can be confusing. You shouldn't try to learn it at the same time you're learning Unity3d. – Peter Duniho Oct 16 '20 at 04:06
  • Yes...class members are accessible always in scope, but local variables (as you have here) are not usable until _after_ their declaration in lexical order, within the same method. – Peter Duniho Oct 16 '20 at 04:07

0 Answers0