![]() But what you should be asking about is the Awake() function, not the variables. You could have 10 instances of the parent class that all have different values for the same variables. A Value Type (bool, float, byte, char, double, int, long, struct) directly stores it’s own data. A Struct is a Value Type, which sits on the Stack, while a Class is a Reference Type, that sits on the Heap. Now, sometimes you'll have a method that you want to have in both the Wolf and Player, let's imagine for example a method TakeDamage(). Variables values are related to an instance of a class, not to the class itself, so they couldn't possibly be inherited. The number of fields you use should be less than 4, and the data should be immutable (unable to be changed). You can for example make a list List combatants and Add your Wolf script and your Player script as if both were combatants scripts (they actually are, and again, they have everything that you've written in combatant in them). If you have a Player myPlayer and you make this check if(this is Combatant) it'll return true. If it's shared by both, write it in the Combatant one. For example, it’s possible to make a door both interactable and damageable by adding more than one interface to the class declaration, separated by a comma. If you have a method that only the player should have then write it in the Player script. One of the advantages of interfaces when compared to similar structural systems, such as inheritance, is that a class can implement multiple interfaces. If you put a method in Combatant, both Wolf and Player have it right now, and you can call it as if it were declare in them. ![]() In fact in the inspector, in your Wolf component, you'lll see every Combatant variable, and after those, the once that are exclusive to the Wolf. So, if in Combatant you write int health =100 both the player and the Wolf will recognize this variable as if it was written above them. A way to think about it once you're getting the hung of it, is as if everything that would be written in Combatant you'd have it a the beggining of both your other scripts.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |