How to edit gameobject's tile with script ?

Hi,
I’ve a little problem.
I want to edit the PolygonCollider2D in “instanced game object” by script.

4993229--487679--upload_2019-9-23_22-16-46.png

public override void GetTileData(Vector3Int location, ITilemap tilemap, ref TileData tileData)
{
    base.GetTileData(location, tilemap, ref tileData);
    string composition = string.Empty;//Makes an empty string as compostion, we need this so that we change the sprite
    for (int x = -1; x <= 1; x++)//Runs through all neighbours
    {
        for (int y = -1; y <= 1; y++)
        {
            if (x != 0 || y != 0) //Makes sure that we aren't checking our self
            {
                if (HasThisTile(tilemap, new Vector3Int(location.x + x, location.y + y, location.z)))
                {
                    composition += 'I';
                }
                else
                {
                    composition += 'O';
                }
            }
        }
    }

    tileData.sprite = holeSprites[0];

    List<Vector2> list = new List<Vector2>();
    list.Add(new Vector2(-.5f, -.5f));
    list.Add(new Vector2(-.5f, .5f));
    list.Add(new Vector2(.5f, .5f));
    list.Add(new Vector2(.5f, -.5f));

    if (composition[1] == 'O' && composition[3] == 'O' && composition[4] == 'O' && composition[6] == 'O')
    {
        list.Clear();
        list.Add(new Vector2(-.3f, -.3f));
        list.Add(new Vector2(-.3f, .3f));
        list.Add(new Vector2(.3f, .3f));
        list.Add(new Vector2(.3f, -.3f));
    }
    else if (composition[1] == 'O' && composition[3] == 'O' && composition[4] == 'O' && composition[6] == 'I')
    {
        list.Clear();
        tileData.sprite = holeSprites[1];
        list.Add(new Vector2(-.3f, -.3f));
        list.Add(new Vector2(-.3f, .3f));
        list.Add(new Vector2(.5f, .3f));
        list.Add(new Vector2(.5f, -.3f));
    }
    else if (composition[1] == 'I' && composition[3] == 'O' && composition[4] == 'O' && composition[6] == 'O')
    {
        list.Clear();
        tileData.sprite = holeSprites[2];
        list.Add(new Vector2(-.5f, -.3f));
        list.Add(new Vector2(-.5f, .3f));
        list.Add(new Vector2(.3f, .3f));
        list.Add(new Vector2(.3f, -.3f));
    }

    tileData.gameObject.GetComponent<PolygonCollider2D>().SetPath(0, list);
}

The sprite’s tile work, but all Tiles have the same gameObject clone.
In this example, i’have 2 tiles, but they have the same PolygonCollider2D.

4993229--487691--upload_2019-9-23_22-52-33.png

Why ? How can have different gameObject ?

Thx for your help :slight_smile:

You are setting properties of the components on the referenced gameObject, you need to wait for the instance to be created first. Most of this logic should be done in StartUp(). GetTileData should only return data about the tile.

See the following as an example:
AnimatedStaticPrefabTile.cs

/*
*   Delve : Scripts.Tiles.Advanced - Dan Flanigan, 2019
*/
using General;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.Tilemaps;

// ReSharper disable once CheckNamespace
[CreateAssetMenu]
public class AnimatedStaticPrefabTile : AnimatedTile
{
    //public Sprite TileSprite;
    [FormerlySerializedAs("TileAssociatedPrefab")] public GameObject tileAssociatedPrefab;

    [FormerlySerializedAs("PrefabLocalZOffset")] public float prefabLocalZOffset;

    [FormerlySerializedAs("UseAbsoluteZOffset")] public bool useAbsoluteZOffset;
    [FormerlySerializedAs("PrefabAbsoluteZOffset")] public float prefabAbsoluteZOffset = -1f;

    public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go)
    {

        //This prevents rogue prefab objects from appearing when the TileBase palette is present
#if UNITY_EDITOR
        if (go != null)
        {
            if (go.scene.name == null)
            {
                DestroyImmediate(go);
            }
        }
#endif

        if (go != null)
        {
            if (useAbsoluteZOffset)
            {
                //Modify position of GO to match middle of TileBase sprite
                go.transform.position = new Vector3(position.x + Globals.PrefabXyOffset
                    , position.y + Globals.PrefabXyOffset
                    , prefabAbsoluteZOffset);

                return true;
            }

            //Modify position of GO to match middle of TileBase sprite
            go.transform.position = new Vector3(position.x + Globals.PrefabXyOffset
                , position.y + Globals.PrefabXyOffset
                , position.z);

            //Set Z
            var localPosition = go.transform.localPosition;
            localPosition =
                new Vector3(localPosition.x, localPosition.y, prefabLocalZOffset);
            go.transform.localPosition = localPosition;
        }

        return true;
    }

    public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
    {
        tileData.sprite = !Application.isPlaying ? m_AnimatedSprites[0] : null;

        if (tileAssociatedPrefab && tileData.gameObject == null)
        {
            tileData.gameObject = tileAssociatedPrefab;
        }

        tileData.flags = TileFlags.InstantiateGameObjectRuntimeOnly;
    }
}

I’m so stupid :frowning:
It’s work now, thx for your help :slight_smile:

No prob at all. The tilebase overrides are a bit odd sometimes.