0
\$\begingroup\$

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 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?? */
    }
\$\endgroup\$

closed as off-topic by MichaelHouse Mar 30 '14 at 15:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – MichaelHouse
If this question can be reworded to fit the rules in the help center, please edit the question.

0
\$\begingroup\$

watt is an instance variable of class First1.

class First1 is a Monobehavior so every "instance" of it must me attached to a GameObject.

The step in the order are:

  1. Get a reference to the GameObject the specific instance you need of First1 is attached to.
  2. Get a reference to the First1 component attached to the GameObject
  3. Access the public field watt

It depends on how your scene is organized, a simple way of doing something like that:

GameObject go = GameObject.Find("name of the GO where First1 component is attached");
First1 comp = go.GetComponent<First1>();
tott = comp.watt;
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.