
I need to switch my tilemap variable ™ inside a script according to an int variable (N), but doing something like TM=GameObject.Find(“Tilemap_” + N) won’t work. How can i do this? I’d prefer not to create a public Tilemap variable for every tilemap that i got.
I’ve seen people say to use “GetComponent()”, but in this way i can’t select wich one i want.
The command you should actually be using is GetComponents(), note the ‘s’. This returns all components that match the type… although since they’re separate objects you might actually want GetComponentsinChildren(), I always have to fiddle with that to get the right one.
In any case that will return an array as I recall and you can just look through that. I believe the array will be in the same order as the hierarchy in the editor although I’m not sure on that.
Honestly though, a public tilemap[ ] variable is probably the way to go if you’re going to be doing this a fair amount. If it is a dynamic number of tilemaps, you can add a simple script that adds or removes from a public List as they are added/removed.
Hi @A_Box
I would do what @Derekloffin said. But if your really need to find children of some object, you can do:
myItem = transform.Find("item_" + 1);
Thanks to both of you! All the methods work, tho i settled for using GetComponentsinChildren() because i only call that function once. Also arrays are always the answer. Thanks again!