Is it possible to change value on base class when the same field gets changed on it's derived class

时间:2018-03-08 22:14:33

标签: c# unity3d inheritance derived-class

I'm using Unity here. My question is, is it possible to change value on base class when the same field gets changed on it's derived class? and vice-versa

I will give an example to make it clear:

public class BaseClass : MonoBehaviour{
    public int x;
}

public class DerivedClass : BaseClass{
    //-------------
}

public class AnotherClass : MonoBehaviour{
    GameObject player = Instantiate(playerModel) as GameObject;  //Assume playerModel is refrenced or something

    DerivedClass example = player.GetComponent<DerivedClass>();

    example.x = 5;
}

In this case player.GetCompenent<DerivedClass >().x will print 5, but player.GetCompenent<BaseClass >().x will print 0. Is there a way to have BaseClass X equal to 5? Without directly setting it to 5

0 个答案:

没有答案
相关问题