0

Is there any way to attach a MonoBehaviour to a tile in Unity, so that every time I place the tile in the scene, it acts like a GameObject with that script?

I went through all of the tile documentation for Unity, and many pages of StackOverflow, but none have an answer that helps me. Also, I know what the Tile class is, that doesn't work with my needs, I just need to know if there's a way to put a MonoBahaviour on a Tile.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • If a Tile is just a c# class then no. You cannot attach like you would with a GameObject. You can have a reference but the component has to be on a GameObject. It is not possible to create a MonoBehaviour with new and just have it in memory, it has to be with AddComponent. – Everts Feb 24 '20 at 05:55
  • you can put one on the overall tile map, but I dont believe you can isolate it onto specific tiles. What exactly are you trying to achieve? – BugFinder Feb 24 '20 at 09:13
  • Ghost, are you just looking for **prefabs** ? Maybe that's what you need – Fattie Feb 24 '20 at 10:02
  • @Fattie I was already using prefabs for things that were more advanced than a sprite or one with a collider, but it started to get disorganized so I went looking to see if there was a way to get a MonoBehaviour on a tile, I believe the accepted answer below was what I needed to know. However, if there is any way to somehow turn an entire prefab into a tile (yes it seems crazy, but I'm expecting anything from Unity at this point) please let me know. – GhostCoder256 Feb 24 '20 at 13:26
  • Ghost, the tile system is really nothing more than "an image". It's just a big complicated way to make "sprites". Really the concept of prefabs does not work with it. I'm pretty sure that using a 'scriptable tile' like in my answer is what you want. But don't forget also you can ***sit things on*** your tiles. A real game object. It could be an invisible one which has the behaviors you wish. Maybe it will help! cheers @GhostCoder256 – Fattie Feb 24 '20 at 13:32
  • @GhostCoder256 I guess you are an experienced programmer coming to grips with Unity. If so I urge you to read THIS by a certain handsome list member https://stackoverflow.com/a/35891919/294884 cheers! – Fattie Feb 24 '20 at 13:34
  • @Fattie I'll take a look at the preload scene, it seems useful, thanks! – GhostCoder256 Feb 24 '20 at 14:40
  • if you have any unity questions @GhostCoder256 its fine to ping me or an experience unity-SO person and its likely there is already an answer to it ! cheers key tip - https://stackoverflow.com/a/36249404/294884 – Fattie Feb 24 '20 at 14:49

1 Answers1

1

"Tiles" are not game objects. So unfortunately you can't "attach" anything to them!

It's possible this example could help

https://docs.unity3d.com/Manual/Tilemap-ScriptableTiles.html

https://docs.unity3d.com/Manual/Tilemap-ScriptableTiles-Example.html?_ga=2.185515305.1216993433.1582538590-1535974129.1582538590

ANOTHER IDEA is you could sit a game object (perhaps an invisible one) ON each and every tile.

(Just do that programmatically, with a loop.)

That invisible game object would "be the actual thing", it would do everything. (Your logic, perhaps clicking, whatever it is you are doing.)

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • That would... actually work. I got so caught up in trying to make the tile itself have functionality, that I completely ignored other approaches. Thanks! – GhostCoder256 Feb 24 '20 at 14:39
  • Unity is incredibly confusing, @GhostCoder256 - particulartly to folks *who are already programmers*. Unity IS NOT an object oriented system. It's just like .. photoshop. Like, there's nothing "object oriented" about Photoshop. You just plop down things on the canvas. its true that with unity, the language you use to create "monovehaviors" happens to be an OO language. But it needn't be, you could use mahcine code. and *unity itself* has nothing to do with "object oriented". it's literally just a canvas that holds things. cheers! – Fattie Feb 24 '20 at 14:51