Custom rule tile. Help please

Hi I’m trying to change the river tiles based on the neighbouring tiles.
There are 3 different types:
1 light green background
2 dark green background
3 brown background

As you can see in this pic, only the dark green background river tiles are shown and I can’t figure out why.

Here are my settings:
(3 dark green
4 brown
x light green
I’ve assigned them to the public tilebases)

And here is the script I’m using for the custom rules:

using UnityEngine;
using UnityEngine.Tilemaps;
using System.Linq;

[CreateAssetMenu(menuName = "VinTools/Custom Tiles/Advanced Rule Tile 2")]
public class CustomRuleTile_Rivers : RuleTile<CustomRuleTile_Rivers.Neighbor>
{
    [Header("Advanced Tile")]
    [Tooltip("If enabled, the tile will connect to these tiles too when the mode is set to \"This\"")]
    public bool alwaysConnect;
    [Tooltip("Tiles to connect to")]
    public TileBase[] tilesToConnect3;
    [Space]
    [Tooltip("Tiles to connect to")]
    public TileBase[] tilesToConnect4;
    [Space]
    [Tooltip("Tiles to connect to")]
    public TileBase[] tilesToConnect5;
    [Space]
    [Tooltip("Check itseft when the mode is set to \"any\"")]
    public bool checkSelf = false;  //true;

    public class Neighbor : RuleTile.TilingRule.Neighbor
    {
        public const int Specified3 = 3;
        public const int Specified4 = 4;
        public const int Specified5 = 5;
    }

    public override bool RuleMatch(int neighbor, TileBase tile)
    {
        switch (neighbor)
        {
            case Neighbor.This: return Check_This(tile);
            case Neighbor.NotThis: return Check_NotThis(tile);
            case Neighbor.Specified3: return Check_Specified3(tile);
            case Neighbor.Specified4: return Check_Specified4(tile);
            case Neighbor.Specified5: return Check_Specified5(tile);
        }
        return base.RuleMatch(neighbor, tile);
    }
    bool Check_This(TileBase tile)
    {
        if (!alwaysConnect) return tile == this;
        else return tilesToConnect3.Contains(tile) || tile == this;
        //.Contains requires "using System.Linq;"
    }
    bool Check_NotThis(TileBase tile)
    {
        if (!alwaysConnect) return tile != this;
        else return !tilesToConnect3.Contains(tile) && tile != this;
        //.Contains requires "using System.Linq;"
    }
    bool Check_Specified3(TileBase tile)
    {
        if (checkSelf) return tile != null;
        else return tile != null && tile != this;
    }
    bool Check_Specified4(TileBase tile)
    {
        if (checkSelf) return tile != null;
        else return tile != null && tile != this;
    }
    bool Check_Specified5(TileBase tile)
    {
        if (checkSelf) return tile != null;
        else return tile != null && tile != this;
    }

}

I’ve got the code from:

And changed some parts of it to fit my needs, but I don’t really understand all of it. :frowning:

Only one way to fix that!!

How to do tutorials properly:

Tutorials are a GREAT idea. Tutorials should be used this way:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don’t make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

If you get any errors, learn how to read the error code and fix it. Google is your friend here. Do NOT continue until you fix the error. The error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

For this particular problem, to reason about what is going on, this process can be helpful too:

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

1 Like
    bool Check_Specified3(TileBase tile)
    {
        if (checkSelf) return tile != null;
        else return tile != null && tile != this;
    }
    bool Check_Specified4(TileBase tile)
    {
        if (checkSelf) return tile != null;
        else return tile != null && tile != this;
    }

The rules you have created for 3 and 4 do not seem to check specifically for the Tiles which have brown or light green background. You may want to review these Rules to check for the Tiles that you want.

I’ve fixed it. Thanks

Could you please post your fix, @Daviiid . Thanks! Learning myself.

Here you go:

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

[CreateAssetMenu(menuName = "VinTools/Custom Tiles/Advanced Rule Tile 2")]
public class CustomRuleTile_Rivers : RuleTile<CustomRuleTile_Rivers.Neighbor>
{
    [Header("Advanced Tile")]
    [Tooltip("If enabled, the tile will connect to these tiles too when the mode is set to \"This\"")]
    public bool alwaysConnect;
    [Tooltip("Tiles to connect to")]
    public TileBase[] tilesToConnect3;
    [Space]
    [Tooltip("Tiles to connect to")]
    public TileBase[] tilesToConnect4;
    [Space]
    [Tooltip("Tiles to connect to")]
    public TileBase[] tilesToConnect5;
    [Space]
    [Tooltip("Check itseft when the mode is set to \"any\"")]
    public bool checkSelf = true;

    public class Neighbor : RuleTile.TilingRule.Neighbor
    {
        public const int Specified3 = 3;
        public const int Specified4 = 4;
        public const int Specified5 = 5;
    }

    public override bool RuleMatch(int neighbor, TileBase tile)
    {
        switch (neighbor)
        {
            case Neighbor.This: return Check_This(tile);
            case Neighbor.NotThis: return Check_NotThis(tile);
            case Neighbor.Specified3: return Check_Specified3(tile);
            case Neighbor.Specified4: return Check_Specified4(tile);
            case Neighbor.Specified5: return Check_Specified5(tile);
        }
        return base.RuleMatch(neighbor, tile);
    }
    bool Check_This(TileBase tile)
    {
        if (!alwaysConnect) return tile == this;
        else return  (tilesToConnect3.Contains(tile) || tilesToConnect4.Contains(tile) || tilesToConnect5.Contains(tile)) || tile == this;
        //.Contains requires "using System.Linq;"
    }
    bool Check_NotThis(TileBase tile)
    {
        if (!alwaysConnect) return tile != this;
        else return !tilesToConnect3.Contains(tile) && !tilesToConnect4.Contains(tile) && !tilesToConnect5.Contains(tile) && tile != this;
        //.contains requires "using system.linq;"
    }
    bool Check_Specified3(TileBase tile)
    {
        if (checkSelf) return tile != null;
        else return tile != null && tile != this && tile == tilesToConnect3.Contains(tile);
    }
    bool Check_Specified4(TileBase tile)
    {
        if (checkSelf) return tile != null;
        else return tile != null && tile != this && tile == tilesToConnect4.Contains(tile);
    }
    bool Check_Specified5(TileBase tile)
    {
        if (checkSelf) return tile != null;
        else return tile != null && tile != this && tile == tilesToConnect5.Contains(tile);
    }

}