- Home /
Assigning/accessing different scripts from the same spot in another script
Hi, I'm making a tower defence game and currently I have one generic turret controller script that all turrets will use which takes care of fire rates targeting etc. However each turret will obviously fire differently. How would I assign the different firing scripts in the one spot.
I don't mind the different scripts functions having the same name like fire(). I was thinking something along the lines of searching through the turrets components for one the ends in fire (use a naming convention) and then assign it like that but i don't think that's going to work.
The games not too far started so I'm open minded to different solutions to this kind of problem that may require large changes, but i would prefer to keep one generic controlling script and one turret type specific one.
Thanks in advance
Answer by sleepandpancakes · Jun 17, 2017 at 06:41 AM
Are you familiar with inheritance? You should always consider structuring your code through inheritance if you want to make something that is a type of another thing. If you make a base Turret class, you could derive from that class for each class defining a turret type. You can then override functions from the base class, which makes it easy to change functionality while keeping the same pattern of calling methods.
Hope this answers your question.
Ok I've had a look into inheritance and yes this seems to be what I'm after/i think it will work. Thank you
Your answer
![](https://koobas.hobune.stream/wayback/20220612124333im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Need help writing script 1 Answer
How can you change the gravity variable depending on your Scene? 2 Answers
How to reduce my script ? 0 Answers
Not changing value after being set. 3 Answers