- Home /
Any way to use addcomponent to add scripts stored in an array?
I've got an array of different scripts that represent a player's cards. I'd like to instantiate a card template prefab, and attach the script at the top of the players "deck" (list of monobehaviors) so that the template takes the appearance and effects of that individual card script.
The issue I'm running into is that addComponent<>() seems to only take a type, rather than being able to come from an array, so DrawnCard.addComponent<>(PlayerDeckScripts[0])
doesn't work. I tried DrawnCard.addComponent<PlayerDeckScripts[0].GetType()
but that doesn't work either. Any ideas?
Thanks
You were so close, if you didn't figure it out yet use DrawnCard.AddComponent(PlayerDeckScripts[0].GetType()) . You may not use generics with variables they must be defined on compilation.
Answer by JDelekto · Jun 24, 2018 at 03:34 PM
DrawnCard.AddComponent(PlayerDeckScripts[0].GetType())
should work for you (not as the generic parameter, but as the method parameter to one of the AddComponent() overloads).