SetTilesBlock at runtime causes changes of the original tiles in the project (design time)

Hello,

I currently working with tilemaps in Unity 2021.2.10. In this context I have to change tiles at runtime via
Tilemap.SetTilesBlock. After running the game in the editor I am experiencing that the changed tiles at runtime are also changed in my project (design time). This behaviour seems really odd to me, because now I have to do implementation to avoid this backchange.

Questions:
1a) Is the described beaviour intended?
1b) (Only if intented) What is the purpose of this feature?
2) Is there an easy way to get around this behaviour aside from making full layer copies at runtime and then showing and modifying these copied layers?

Likely this is “as designed.” It has to do with how Unity blurs the distinction between editor scripts and runtime scripts.

If it’s not intended it may be that you are modifying the prefab you are using instead of the actually-instantiated thing.

I have not fiddled with tilemaps enough recently to confidently say what is happening.

I see. The editor stuff also may have to change tiles so they probably use the same API. But in theory it should be possible to distinguish runtime calls from editor (design time) calls.

I don’t use a prefab in my case, so this can be excluded as a valid reason.

Without knowing exactly what you’re doing here, and you probably know this: if you’re changing any values of properties or fields of the tile asset itself then those changes persist after play mode is exited in the Editor. If you want to change color, etc., affect the tilemap via code and not the asset.

Thanks Kurt-Dekker and vonchor for your answers. I found the solution of my problem. I made a small mistake with big impact. My script used the attribute…

[ExecuteInEditMode]

…and this explained the behaviour. I used the attribute for some testing before and forgott to remove it. Now it works as desired.

Sorry for the inconvenience :sweat_smile:

1 Like

Not at all! Your original question is excellent, and the ExecuteInEditMode decorator you list above is yet another way that editor and runtime come so awesomely close together, which is what makes editor scripting in Unity so incredibly powerful. But it can boggle you sometimes trying to figure out why something changed.

Also, not sure if you’re using source control, but source control can help you instantly spot code that makes unintended changes to existing asses, helping to identify these things. Here’s my standard pro-source-control blurb:

Please consider using proper industrial-grade source control in order to guard and protect your hard-earned work.

Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

You can also push git repositories to other drives: thumb drives, USB drives, network drives, etc., effectively putting a complete copy of the repository there.

As far as configuring Unity to play nice with git, keep this in mind:

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

Here’s how I use git in one of my games, Jetpack Kurt:

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

Using fine-grained source control as you work to refine your engineering:

https://discussions.unity.com/t/826718/2

Share/Sharing source code between projects:

https://discussions.unity.com/t/719810/2

Setting up an appropriate .gitignore file for Unity3D:

https://discussions.unity.com/t/834885/5

Generally setting Unity up (includes above .gitignore concepts):

https://thoughtbot.com/blog/how-to-git-with-unity

It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place.

“Use source control or you will be really sad sooner or later.” - StarManta on the Unity3D forum boards

Thanks fĂĽr your advice, Kurt-Dekker. I indeed already use git as source control for my project. And I totally agree with you on the usefullness of source control for software development in general. The most important aspect of source control is to take away any fear of changing your own project, because you can always go back to an earlier version of it with source control. But you already mentioned this in one or more of your linked posts :slight_smile:

1 Like