Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Awake and Update not running

Discussion in 'Scripting' started by Nt91, Aug 24, 2018.

  1. Nt91

    Nt91

    Joined:
    Aug 24, 2018
    Posts:
    1
    Hi!

    I've been tying to fix this forever now to no avail.
    For some reason the Awake() and Update() functions don't get called.

    The Array contains one Ability (a scriptable object) and the foreach loop is supposed to instantiate a copy and then place it inside the dictionary. But then when I check it, it's empty and the Debug message I placed there won't print. I really don't understand why. I've done exactly the same thing in another script that works fine. It just doesn't seem to work here...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using System.Linq;
    6.  
    7.  
    8. public abstract class AbilityController : MonoBehaviour {
    9.  
    10.     [SerializeField]private Ability[] _abilities;
    11.  
    12.     private readonly Dictionary<Type, Ability> _abilityDictionary = new Dictionary<Type, Ability>();
    13.  
    14.     public Ability currentAbility;
    15.  
    16.  
    17.     void Awake () {
    18.  
    19.         Debug.Log ("here");
    20.  
    21.         foreach (Ability ability in _abilities) {
    22.  
    23.             Ability instance = Instantiate (ability);
    24.             instance.Initialize (this);
    25.             _abilityDictionary.Add (instance.GetType (), instance);
    26.  
    27.             if (currentAbility != null) continue;
    28.             currentAbility = instance;
    29.             currentAbility.Enter ();
    30.  
    31.         }
    32.          
    33.      
    34.     }
    35.  
    36.  
    37.     void Update () {
    38.         Debug.Log ("here");
    39.  
    40.      
    41.     }
    42.  
    43.     public T GetAbility<T>() {
    44.         Type type = typeof(T);
    45.         if (!_abilityDictionary.ContainsKey (type)) throw new NullReferenceException ("No ability of type: " + type + " found");
    46.         return (T)Convert.ChangeType (_abilityDictionary [type], type);
    47.     }
    48.  
    49.     public void TransitionTo<T>(){
    50.         currentAbility.Exit ();
    51.         currentAbility = GetAbility<T> () as Ability;
    52.         currentAbility.Enter ();
    53.     }
    54.  
    55.     public void PrintList(){
    56.      
    57.         Debug.Log (_abilityDictionary.Count);
    58.     }
    59.  
    60. }
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    That class is abstract. Can you show us the derived class as well?
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Why is this an abstract class?
     
    Doug_B likes this.
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,533
    What I find strange is that your class is marked abstract. Meaning you could not do „new AbilityController()“. Of course being a MonoBehaviour you can still instantiate it but I have no idea how this would behave under Unity, given that an abstract class cannot really be instantiated. I have a hunch that could be the issue here.
     
    Joe-Censored likes this.