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?