我如何获得不同类的价值 [英] how do I get the value of a different class

查看:101
本文介绍了我如何获得不同类的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误,我无法从不同的班级获得价值..



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 and MonoBehavior do not contain a variable called watt and the only class that does (First1) is not related to Second2
Instead, derive Second2 from First1 rather than MonoBeahviour:
public class Second2 : First1 {
        private List<ISecondListener> listeners = new List<ISecondListener>();
        public int tott;

        public void cek (){
            tott = watt; 
        }


这篇关于我如何获得不同类的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆