Can I attach the same script to two different nodes? Can I do it without major problems?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gamersen

I’ve got two nodes which are very similar. They are probably going to use the same if not very similar code. Can I attach the same script to both of them? Would it be better to create two scripts for each of them?

:bust_in_silhouette: Reply From: Ertain

Why not have scripts on the two nodes inherits from the original script? The two nodes can share the same code, yet still have code which differentiates them. Look to the documentation for more information.

:bust_in_silhouette: Reply From: whiteshampoo

Short answer:

Yes, you can definitly do this


Long answer:

Depends.
If you have 2 Nodes, that should do exactly the same, but look different, then it’s ok.
If they should behave different, then you should probably use inheritance.
Here is a nice tutorial: KidsCanCode-Tutorial


EDIT:
To make it more clear:
If you have to write 2 times the very same code, then you are doing something wrong.
If you want to change the behavior of something and have to replace it on 2 places in your code, then you should have made a function for that, or have inherited another script/node/whatever.
I hope you understand what i mean. If not, feel free to ask!