Click here to Skip to main content
15,881,766 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i get error , i cant get the value from different class..

C#
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 want to get the value var watt and assign to tott.. (tott = watt)

C#
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?? */
        }
Posted

1 solution

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:
XML
public class Second2 : First1 {
        private List<ISecondListener> listeners = new List<ISecondListener>();
        public int tott;

        public void cek (){
            tott = watt; 
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900