Heritage, child variable not working

This problem is on Unity, but I think I am just doing the c# side wrong.

EDIT

It looks like by doing the code and storing a child Class B of parent Class A, and by saying is a Class A type, by modifing my variable of type Class A containing Class B I modify some sort of hybrid Class A/B that doesn’t represent my real Class B Script

What I do is, having multiple script on different prefabs. Each of those script represent an item and all have has parent Usable which is a class that I actually use like an interface, but that will in the future get some stuff.

The full WeaponLaser and Usable script is below

When the player go over a drop, I instantiate the gameObject containing the script like this (using prefab)

GameObject Item = Instantiate(droppedItem, transform.position, Quaternion.identity);
Item.transform.parent = transform;
usableItem = droppedItem.GetComponent<Usable>();
usableItem.OnUsed += ReleaseItem;

and Use the item like this

if (usableItem != null)
    usableItem.Use(firePoint.position);

The thing is, it looks like the script I call when I do Use() is another version.

I mean, If I set int fireCurrentShoot = 10; on top of the script WeaponLaser and then trought code in the Start for exemple I do fireCurrentShoot = 2;

It will work on the inside the script WeaponLaser, but when I call it using the above code

    if (usableItem != null)
    usableItem.Use(firePoint.position);

It will show fireCurrentShoot = 10

END EDIT

Hello,

I have a problem with heritage I don’t understand, I cleaned all my class, and still I can’t find why.

I have a class A :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Usable : MonoBehaviour
 {
     protected virtual void Start()
     {
     }
 
     protected virtual void Update()
     {
     }
 
     public virtual void Use(Vector3 pos)
     {
     }
 
     protected virtual void Used()
     {
     }
 }

and a class B

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WeaponLaser : Usable
 {
     const int SHOOT_AVAILABLE = 5;
     const float FIRE_COOLDOWN = 1;
     float fireCurrentCooldown = 0.0f;
     int fireCurrentShoot = 0;
 
     protected override void Start()
     {
        base.Start();
        Debug.Log("start");
        fireCurrentShoot = SHOOT_AVAILABLE;
        Debug.Log("fireCurrentShoot" + fireCurrentShoot);
     }
 
     protected override void Update()
     {
         Debug.Log(fireCurrentShoot); // value is = 5
         base.Update();
     }
 
     public override void Use(Vector3 shootPosition)
     {
         Debug.Log(fireCurrentShoot);// value is = 0
         base.Use(shootPosition);
         base.Used();
     }
     
     void FireCooldown()
     {
 
     }
 }

when I call Use, my Debug.Log of booth value give 0… but I am expecting to have fireCurrentShoot = 5

I call it like this *:

    usableItem = droppedItem.GetComponent<Usable>();
usableItem.Use(firePoint.position);

why he is equal to 0?

You probably called Use before Unity runs the Start method of your class. Start is called before the next Update. So usually the next frame. If you want to initialize the objects own properties you should use Awake. Awake will be called right after the object has been deserialized and will run even before Instantiate returns the object.

Also when you debug such cases i strongly recommend that you print some meaningful log message which can be identified uniquely

protected override void Update()
{
    Debug.Log("Update: fireCurrentShoot: " + fireCurrentShoot + " Obj: " + gameObject.name, gameObject);
    base.Update();
}

Note that i also print the object name so it’s more clear where this log comes from. Also note that i pass “gameObject” as second parameter. The second parameter is a “context” parameter. When you click on the log message in the console once that object will be highlighted in the project or hierarchy panel. Maybe you reference the wrong object? For example it’s possible that you call “Use” on a prefab. However a (non instantiated) prefab doesn’t get it’s Start or Update method called.

Finally pay close attention to the order in which the logs appear. If still in doubt you can also add Time.time to the log message or Time.frameCount.