Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Inheritance

Discussion in 'Scripting' started by DanielCuartasQ, Nov 18, 2022.

  1. DanielCuartasQ

    DanielCuartasQ

    Joined:
    Mar 20, 2022
    Posts:
    4
    I'm feeling pretty bad right now, I'm stuck at something that looks pretty basic. I'm new at this... but not that new.

    I'm studying inheritance, so I came with an example like this:

    upload_2022-11-18_14-35-53.png

    I want to create a base class called Animal, and three classes that inherits from it: Cat, Dog and Chick

    -All animals have a name
    -All animals can jump, but a Cat jumps higher
    -Every animal makes a different sound.

    you select first the animal, and then jump or make a sound.

    I just don't know where to start.

    I made this scripts:
    UIController - Here I read the buttons and change a string variable selectedAnimal and store the animal

    upload_2022-11-18_14-41-24.png

    upload_2022-11-18_14-41-0.png

    made these scripts as well:

    Animal (Parent)
    Dog (Child)
    Cat (Child)
    Chick (Child)

    Then I planned to make a Jump() and Sound() method but how can I call Cat.Sound() o Dog.Sound() using selectedAnimal?

    Why am I stuck at this!?

    could you please teach me how should I plan to solve this kind of problems? what goes into your mind when solving this kinds of problems?

    Thank you!
     

    Attached Files:

  2. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    Look into composition over inheritance.
    You can have a component called animal and then you build a dog by adding other components.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,862
    You don't even need inheritance to do this. Just a general purpose component would do the job.

    Also a string isn't useful for recording what animal you have selected. You want to have actual references to the components of these animals in order to make any use of them.
     
    Chubzdoomer likes this.
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    That's is a terrible place to use inheritance, since you could easily have one class named animal, with strings for "name", "sound" and "jumpHeight". To make a dog, set them to dog values, and so on.

    But let's say you just want to play with inheritance. Fine. Give everyone a Sound() function -- Animal and all 3 subclasses get it. It needs to be named exactly
    void Sound()
    in all 4 places. In C# you'll also need to add virtual and override to finish it up (sorry -- C# can be awkward. but it's easy to find example of which word goes where).

    Then make an Animal variable and have it point to different subclasses:
    Code (CSharp):
    1. Animal aa = new Cat();
    2. aa.Sound(); // meaio
    3. aa = new Dog();
    4. aa.Sound(); // Arf
    Inheritance made it legal for aa to point to a non-Animal. Whoa! Normally that would be illegal, like string w=Vector3.zero;. But inheritance says "oh, a Cat is a type of Animal", so allows it. Then down below,
    aa.Sound()
    magically (because of inheritance and polymorphism) says "oh -- you want me to call the Sound for whatever subtype you are now".
     
    Chubzdoomer likes this.
  5. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    Problem is with inheritance sooner then later you want something to Maui tjat isn't a Cat or even an Animal and that's were composition comes in
     
  6. CodeKiwi

    CodeKiwi

    Joined:
    Oct 27, 2016
    Posts:
    119
    Composition/components over inheritance is normally preferred as mentioned above. But if you did want to use this as an inheritance test then it might look like the following (it’s more compact than it should be to avoid taking up too much space in the forum).

    Code (CSharp):
    1. public abstract class Animal : MonoBehaviour
    2. {
    3.     public string localizedName;
    4.     public AudioSource audioSource;
    5.     public AudioClip audioClip;
    6.     protected void Jump(float height) => Debug.Log($"Jump {localizedName} {height}");
    7.     public virtual void Jump() => Jump(1);
    8.     public virtual void Sound() => audioSource.PlayOneShot(audioClip);
    9. }
    10. public class Dog : Animal { }
    11. public class Cat : Animal { public override void Jump() => Jump(2); }
    12. public class Chick : Animal { public override void Sound() => Debug.Log("Chick Sound"); }
    13.  
    14. public class UIController : MonoBehaviour
    15. {
    16.     public Animal selectedAnimal;
    17.     public List<Animal> animals;
    18.  
    19.     // connect in button OnClick
    20.     public void Jump() => selectedAnimal?.Jump();
    21.     public void Sound() => selectedAnimal?.Jump();
    22.     public void SelectAnimal(int index) => selectedAnimal = animals[index];
    23.  
    24.     // polymorphism example
    25.     public void AllJump()
    26.     {
    27.         foreach (var animal in animals)
    28.         {
    29.             animal.Jump();
    30.         }
    31.     }
    32. }
    33.  
    Some issues of using inheritance from above include:
    • Chick overrides Sound() and no longer requires an audioSource or audioClip but they still exist.
    • Multiple inheritance isn’t supported e.g. FlyingAnimal:Fly(), WaterAnimal:Swim() duck can fly and swim. Normally you could get around this with interfaces e.g. class Duck : MonoBehaviour, IFlyingAnimal, IWaterAnimal. Unfortunately interfaces don’t really work well with components (can't assign IAnimal to UIController.selectedAnimal or UIController.animals in the editor).
     
  7. DanielCuartasQ

    DanielCuartasQ

    Joined:
    Mar 20, 2022
    Posts:
    4
    @Max-om and @CodeKiwi
    It is exactly that, I just wanted to test if I was able to put into practice what I learned about inheritance, and that's it, more like a proof of concept.

    Thank you!!!