Advertisement
LeeMace

Inheritance and Polymorphism

Jun 26th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.66 KB | Gaming | 0 0
  1. //base class
  2. public class BaseCube : MonoBehaviour
  3. {
  4.    public virtual void DoStuff() {
  5.     }
  6. }
  7.  
  8. //child 1
  9. public class Cube1 : BaseCube
  10. {
  11.     public override void DoStuff() {
  12.         base.DoStuff();
  13.          transform.Rotate(0, 1000, 0);
  14.     }
  15. }
  16.  
  17. //child 2
  18. public class Cube2 : BaseCube
  19. {
  20.     float scaleFactor = 1.01f;
  21.     public override void DoStuff() {
  22.         base.DoStuff();
  23.  
  24.         transform.localScale *= scaleFactor;
  25.     }
  26. }
  27.  
  28. //child 3
  29. public class Cube3 : BaseCube
  30. {
  31.     [SerializeField] Renderer myRenderer;
  32.     public override void DoStuff() {
  33.         base.DoStuff();
  34.  
  35.         myRenderer.material.color = Color.red;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement