- Home /
How to attach different scripts to one script for different prefabs
My question arises when I started to work on a second enemy for my game. Essentially, I want to be able to use one script for the decision making of all enemies, while each prefab has a different script concerning what moves correspond to those decisions.
I was wondering if its possible to 'drop' scripts the same way transforms can be dropped in to access functions on one script with the same name as functions on a different script.
I've included a part of my code to hopefully make my question more clear:
What I would like is that the script 'EnemyMovesScript does not have to only refer to one script, but that for each prefab I can attach a different script describing the moves that are available to that enemy prefab.
Thanks in advance!
Answer by GameBrews · Sep 08, 2021 at 11:21 PM
I'm not sure I understood you correctly.
is this the structure you want?
each enemy prefab contains a Decision making script and a Movement script.
where the movement script for each enemy is different?
is so, you could use inheritance. Create an abstract base movement script, lets call it BaseMovement
detailing all types of movement, and let each enemy have its own derived class that implements all of BaseMovement
class abstract methods.
that way, the decision making script could have a reference of type BaseMovement
, and initializing it with GetComponent()
will fetch a reference to the derived class in the current GameObject
.
Hope that helps :)
Your answer
Follow this Question
Related Questions
Simple 2D Enemy AI 3 Answers
How can I apply a text variable to a prefab 2 Answers
i need help creating a mario like enemy ai for the roll a ball game 1 Answer
Bullet Prefab spawning in multiple random positions and rarely the correct one (Unity 3D) 1 Answer
Script on Prefab is not changeable without changing all other instaniated objects 1 Answer