Search Unity

Spawning Random Enemies with Polymorphism

Discussion in 'Scripting' started by artistshc, Apr 27, 2015.

  1. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Hi everyone. I am creating a game and I am pretty much done. This game is for a class though and I just realized that I need to add some Polymorphism to this game. I have already created a way to make levels random, so I could probably do it in a similar way, but I need to use Polymorphism. I know how to do this in JAVA, but not in C#/Unity. Do I need to create a separate class for each enemy? Anyone familiar with this? if so, could you push me in the right direction?

    //Should I do something like this?

    Code (CSharp):
    1. void Start () {
    2.         //Should I do this with every enemy
    3.         Enemy myEnemy = new devil();
    4.         Devil myDevil = (Devil)myEnemy;
    5.  
    6.         //myDevil is just a placeholder, I would make a randomizer variable and stick it in there.
    7.         Instantiate (myDevil, EnemySpawner.transform.position, EnemySpawner.transform.rotation);
    8.    
    9.     }
    Thank you in advance.
    Rachel Goldstein
     
  2. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Basic polymorphism is pretty easy. Just make your enemies derive from a base class called "Enemy".

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Enemy : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.    
    15.     }
    16. }
    17.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Devil : Enemy {
    5.  
    6. }
    7.  
    You will also want to look into abstract and override. Here's a link from MSDN: https://msdn.microsoft.com/en-us/library/ebca9ah3.aspx
     
    Kiwasi, artistshc and SubZeroGaming like this.
  3. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Thank you so much Cody!!! I will look into abstract and override. I might post back again if I don't understand it. Thank you!!!

    Rachel

     
  4. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    I'm just not getting it. Could you expand a little bit with an example....I'm sorry!
     
  5. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    I can certainly do a bit more for you after I finish up my dinner. It's quarter past five here on the west coast and I'm starving. If nobody beats me to it I should have a little something for you in an hour :)
     
  6. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Hopefully this will clear things up a bit more for you:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemy : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.        
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.        
    14.     }
    15.  
    16.     public virtual void Attack() {
    17.         //Let's say every enemy can do a melee attack; so in here we would code up our melee attack.
    18.     }
    19. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Devil : Enemy {
    5.     public override void Attack() {
    6.         //Let's say that devils have a ranged fire attack; we use this override void to call that attack instead of the attack method from the base class.
    7.         //This method overrides the base melee attack and replaces it with a fireball or something
    8.         //This is basic polymorphism. Classes that inherit from one another, but can have different results when calling the same method
    9.     }
    10. }
    11.  
     
    artistshc likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You've already introduced inheritance from MonoBehaviour.

    You've got polymorphism with Component and MonoBehaviour. How about a simple script to disable every component on a given GameObject?
     
    hamsterbytedev and artistshc like this.
  8. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Oh wow! Thank you so much...this helps so much! Thank you soooo much! I really appreciate your help!
     
  9. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    This is a very good idea. I used this for part of my game where parts of the players body had to show up and disappear. I didn't think about using that here until now. Thank you!!
     
  10. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Very true. I was kind of throwing it down quick and dirty haha.