Passing a script of a game object to a separate script on a different game object

Hi i used to make some games some 4 years ago and i started to get back into it now, i am making a sort of RTS game where units move through tiles instead of a normal open map.
The issue i currently have is that each tile has a script witch has a list of adjacent tiles that units can reach from a given tile (this is an attempt to mimic road connections to those tiles) but i dont know a way to pass this script to the unit sitting on the tile once it moves to a new tile.

In essence i have Game object 1 (unit) with Script 1 ( unit movement script) that gets the information about its current tile passed to it from Script 2 (this is the tile handler script) when it is instantiated because the tile acts as a spawner so it has access to all of the data that i need to handle unit movement.
What i need is for Script 1 to see Script 3 (tile handler script for the tile the unit is moving to) and read its adjacent tiles list so it can move from this new tile

If anyone has an idea of how to do this i would be very thankful.
Another idea i had is to consolidate all of the tile connections into a single list that would act as a global manager but i am not exactly sure how i would implement that

Ideally you should instantiate all the tiles from a script at the start of the game and hold a reference to each tile in an array, it’ll then be easy to efficiently select neighboring tiles. You could even consider not having a script on each tile and instead managing all the units across all tiles from a single script. This will be a little more performant.

You can hang all this stuff on various Components in the Unity scene, but then everything you do is tightly bound to the Unity engine.

Instead for a tiled context, just store all your own data in whatever 2D storage mechanism you like and use Unity as a presentation layer only.

Tile-based / grid-based 2D games: match3, tetris, chips challenge, rogue, etc:

For any tile-based game such as Match3 or Tetris or a grid-based Roguelike, do all the logical comparisons in your own data storage mechanism for the tiles, such as a 2D array of tiles.

Otherwise you needlessly bind your game logic into Unity objects and the Unity API, making it about 10x more complicated than it needs to be.

If you have no idea how to work with 2D arrays, hurry to some basic C# tutorials for the language portions of it, then look at any good tutorial that uses a 2D array

Here is my Match3 demo using this technique of storing data in a grid. Full source linked in game comments.

https://kurtdekker.itch.io/match3-demo

It stores all of its data in a 2D array:

PieceController[,] Board;

This allows for easy simple checking in code, not relying on anything like physics.

You should strive to use that pattern for all logic, then only use Unity to present what is happening in the game logic.

This is what i was aiming for, the game is already in 2d and i am not relying on any physics as it would just add needles complexity. I have spent most of the day trying to figure something out and the best i have for now is an array list that stores lists of Transforms of the adjacent tiles and the names of the tiles that belong to those transforms with the rule that the index for a given tile is the same across the two reference lists
this sounds a little confusing so ill give an example

ArrayList [Transform array[tile_1_trans, tile_2_trans, tile_3_trans] tile_0_ajd, String array[tile_1_adj, tile_2_adj, tile_3_adj] tile_0 ] tileNameTransform;

this structure should allow me to find both transforms (x and y co-ords of tiles) and also keep up with what tile a given unit is standing on, also making a global script that handles this means that i dont have to pass a new script every time a unit moves but just read what tile the unit got moved to from the string array.
the main trick to keeping this thing readable is to keep the two “sister arrays” in sync and next to each other
I apologize if this solution makes some people pull their hair out but i cant think of anything that works better than this, i dont exactly have a formal education in programing im self taught trough the internet so if i am missing anything i would appreciate any feedback.

also i am not exactly sure how to open your game, my unity hub pops up when i try to open the file but then nothing happens

The answers given may provide you with ideas. I wanted to iterate the concept of writing the code first. Create a class and add methods that provide answers to questions you need to ask. Game.NumberOfTiles, Game.IsTileOccupied, Game.GetAdjacentTiles. Obviously you don’t need to name the class Game and you generate whatever properties and methods you need. This should help insure that you can obtain the values you need and provides a way to test it using Debug.Log statements.

Don’t worry about optimizing, worry about obtaining the values and operations you need. You shouldn’t need 2 sister arrays ever. I believe I would start with a collection of Tile objects. A collection can be converted to an array when needed and can be queried with LINQ making “how many have been tagged” or “change all the blue ones to red” easy and understandable.

2 Likes

Yes this ^ ^ ^ ^ … I call this “write the API first…”

What is your game going to need from the dungeon world, the items, etc.

  • what item is here?
  • can I walk here?
  • are there monsters here?
  • can I line-of-sight (LoS) through this square?
    etc

Keep in mind the “can I walk here” might be instead structured as “I walk here, what is the result?” and that could be blocked, pass through, activate trap, trigger another mob to spawn somewhere nearby, etc.

alright thanks for all the responses ill get back to this discussion when i get some time to write more of the code, granted i have managed to make the global tile manager work so this helps a lot already :slight_smile: