我如何获得不同类的价值 [英] how do I get the value of a different class
本文介绍了我如何获得不同类的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到错误,我无法从不同的班级获得价值..
i get error , i cant get the value from different class..
public class First1 : MonoBehaviour {
private List<IFirstListener> listeners = new List<IFirstListener>();
public int watt;
void OnTriggerEnter2D(Collider2D theCollider)
{
try {
CharacterA characterA = theCollider.GetComponent<CharacterA>();
this.watt += characterA.watt;
for (int i = 0; i < listeners.Count; i++) {
listeners[i].OnBaterryPluggedIn(this, characterA, characterA.watt);
}
}
catch { /*On error do nothing*/ }
}
void OnTriggerExit2D(Collider2D theCollider) {
try {
CharacterA characterA = theCollider.GetComponent<CharacterA>();
this.watt -= characterA.watt;
if (characterA != null) {
for (int i = 0; i < listeners.Count; i++) {
}
listeners[i].OnBaterryPluggedOut(this, characterA, characterA.watt);
}
}
catch { /*On error do nothing*/ }
}
i希望获得值var watt并分配给tott ..(tott = watt)
i want to get the value var watt and assign to tott.. (tott = watt)
public class Second2 : MonoBehaviour {
private List<ISecondListener> listeners = new List<ISecondListener>();
public int tott;
public void cek (){
tott = watt; /* <--- how i can get the value from var watt at class First() to var tott?? */
}
推荐答案
你不能 -Second2
和MonoBehavior
不包含名为watt
的变量,并且是唯一可以执行此类的变量(First1
)与无关> Second2
相反,派生Second2
来自First1
而不是MonoBeahviour
:
You can't -Second2
andMonoBehavior
do not contain a variable calledwatt
and the only class that does (First1
) is not related toSecond2
Instead, deriveSecond2
fromFirst1
rather thanMonoBeahviour
:
public class Second2 : First1 {
private List<ISecondListener> listeners = new List<ISecondListener>();
public int tott;
public void cek (){
tott = watt;
}
这篇关于我如何获得不同类的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文