- Home /
Is there a way to make a variable correspond to multiple different scripts?
I have a game I'm making in unity and in that game when the player attacks it gets the enemy's script and executes Die(). However, I have multiple scripts for the different types of enemies (EX: jumpers, chargers) but, they all have Die(). Is there anyway to declare a variable that can be set to any script or am I going to have to use if(){}? I want something kind of like a var except it doesn't have to work for anything except scripts. Thank YOU! (I understand if there is nothing like this, it was a longshot)
Hi, looks like you could use a base class containing Die()! Search for "inheritance" in your favorite progra$$anonymous$$g language. https://en.wikipedia.org/wiki/Inheritance_%28object-oriented_program$$anonymous$$g%29
The problem you are trying to solve here comes up a couple times a month. Here is a combined list of solutions that have been presented for this problem:
Having a base class and deriving the individual enemy behavior by overriding.
Answer by Detinator10 · Aug 28, 2014 at 01:33 AM
I ended up using a base class and then overriding it.
Your answer
Follow this Question
Related Questions
How to assign a private transform of my player in script? 1 Answer
assign a script to a variable 1 Answer
Updating variable on another script. It is not working. 2 Answers
accessing a variable from another script. 3 Answers
Can I make variables visible to other scripts without making them visible in the Inspector? 1 Answer