Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Multiple Enemy Types - C# Subclassing Question about overriding/setting variables

Discussion in 'Scripting' started by Squabbler, Oct 24, 2013.

  1. Squabbler

    Squabbler

    Joined:
    Jan 11, 2013
    Posts:
    11
    Let's say, to keep the example simple, I have two (2) different enemy types. One has an attack that has a 2 second cool down, and the other has an attack with a 5 second cool down.

    Enemy A -> 2 second cool down
    Enemy B -> 5 second cool down

    Both Prefabs will use the same BasicEnemyController script for movement and logic for when to attack. However, I need things such as cool down, attack duration, as well as other things, to be different.

    The variables in question are:
    • AttackDuration
    • CooldownDuration

    I've tried setting them with a Start method in the subclass and a simple override method (seen below), but I still can't get the variables in the main class to change. Is there some trick to this?


    I'm dumb :(... I had the SubClass script unchecked in the inspector.
     
    Last edited: Oct 25, 2013
  2. Apox2060

    Apox2060

    Joined:
    Feb 26, 2013
    Posts:
    8
    Where are you setting your attackVariables?

    I see a function to set them but doesn't get called on start(). If you are setting them through the unity editor then thats fine otherwise I believe your cooldownTimer will be 0 therefore your attackTimer will always be 0 which is probably why you are getting spammed with "Attack now" and not seeing the attack animation because of the same reason. I would suggest calling SetAttackVariables() on start.

    You also call the function Attack() in update but I don't see a function to match that within your enemy class?

    If you can provide me with answers to these I might be able to help you out a bit more
     
  3. Squabbler

    Squabbler

    Joined:
    Jan 11, 2013
    Posts:
    11
    Yeah, sorry. I feel so dumb. I had it right from the beginning, but the script was unchecked in the inspector lol.... Everything works as it should. If anyone needs help this is how it's done.

    MainClass
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MainClass : MonoBehaviour {
    6.  
    7.     protected float myVariable;
    8.  
    9.     protected virtual void Start () {
    10.  
    11.         // global default for class
    12.         myVariable = 1.0f;
    13.  
    14.     }
    15.  
    16. }
    17.  
    SubClass
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SubClass : MainClass {
    6.  
    7.     protected override void Start () {
    8.  
    9.         base.Start();
    10.  
    11.         // override for specific sub class
    12.         myVariable = 1.5f;
    13.  
    14.     }
    15. }
    16.  
    I removed the code from the original post since it's unnecessary.

    --------------

    A lot of the methods I left out of the code I submitted here for length purposes. But, all of them are in the actual script file.
     
    Last edited: Oct 25, 2013