Prefab reference in script has 0 and null values for class variables C#

Hello,

I’m having a problem where my prefab GameObject has 0 and null values for all of its class variables. I’m trying to get the value of a variable called cooldown in my code, so I can use that value in my MagicController class.

When I Instantiate the prefab all the values are there, it’s just an issue with the object reference in MagicController itself.

I’ve tried looking all over for answers and can’t seem to find any, any help would be greatly appreciated. The three scripts i’m using are below.

Thank you in advance.

    using UnityEngine;
    
    public class MagicController : MonoBehaviour
    {
        [SerializeField]
        private Transform abilitySpawn; // Position of where the ability will spawn
    
        [SerializeField]
        private GameObject prefab;
    
        private bool attackReady;
    
        private float lastAttack, cooldown;
    
        void Start()
        {
            lastAttack = 0.0f;
            attackReady = true;
    
            cooldown = prefab.GetComponent<IAbility>().cooldown;
    
            print(cooldown);
        }
    
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.F) && attackReady)
            {
                cast();
    
                lastAttack = cooldown;
                attackReady = false;
            }
    
            if (lastAttack > 0)
            {
                lastAttack -= Time.deltaTime;
            }
    
            else
            {
                attackReady = true;
            }
        }
    
        void cast()
        {
            Instantiate(prefab, abilitySpawn.position, abilitySpawn.rotation);
        }
    }

    using UnityEngine;
    
    public class Fireball : MonoBehaviour, IAbility
    {
        public string abilityName { get; set; }
        public string description { get; set; }
    
        public float damage { get; set; }
        public float speed { get; set; }
        public float cooldown { get; set; }
        public float manaCost { get; set; }
    
        public int duration { get; set; }
    
        public Rigidbody2D rb { get; set; }
    
        // Start is called before the first frame update
        void Start()
        {
            abilityName = "Fireball";
            description = "Cast a small fireball at your enemy";
    
            damage = 20.0f;
            speed = 30.0f;
            cooldown = 3.0f;
            manaCost = 10.0f;
    
            duration = 3;
    
            rb = gameObject.GetComponent<Rigidbody2D>();
            rb.velocity = transform.right * speed; // Current speed * forward direction * how fast you want to go
        }
    
        private void OnTriggerEnter2D(Collider2D col)
        {
            Enemy enemy = col.GetComponent<Enemy>();
    
            if (enemy != null)
            {
                enemy.takeDamage(damage);
            }
    
            Destroy(gameObject, duration);
        }
    }

public interface IAbility
{
    string abilityName { get; set; }
    string description { get; set; }

    float damage { get; set; }
    float speed { get; set; }
    float cooldown { get; set; }
    float manaCost { get; set; }

    int duration { get; set; }

    UnityEngine.Rigidbody2D rb { get; set; }
}

This is because your values are set during runtime. The prefab is just a reference so the values are null or default. There is no serialization on the values because you are using auto properties.
The values are only populated when the code is executed and that isn’t done until the object is active in the scene.