0

example I have class name "PlayerClass" and "StatClass"

PlayerClass

public float health = 100 ;
public float experience;

StatClass

PlayerClass playerclass = new PlayerClass()
public float health2;
public float experience2;
health2 = playerclass.health;
experience2 = playerclas.experience;

usually I have to create "PlayerClass playerclass = new PlayerClass()"

the result will be the same health2 and experience2; but whenever i made a change on healt from player class with code of course, the result healt2 from StatClass will be the same it was 100

Sory for my bad english.

----------------- Edited -----

GameControl

public class GameControl : MonoBehaviour {

    public static GameControl control;

    public float health = 100;
    public float experience = 1000;
    // Use this for initialization

    void Awake () {
        /*
         * Awake Start Before Start() Happen
         */
        if (control == null) {
            DontDestroyOnLoad (gameObject);
            control = this;
        } else if (control != this) {
            Destroy (gameObject);
        }
    }
}

GetHealth

public class GetHealth : MonoBehaviour {

    public static GetHealth getHealth;
    GameControl gameControl = new GameControl();
    //GameControl gameControl;

    public static float health;
    Text text;

    void Awake () {
        /*
         * Awake Start Before Start() Happen
         */

        if (getHealth == null) {
            DontDestroyOnLoad (gameObject);
            getHealth = this;
        } else if (getHealth != this) {
            Destroy (gameObject);
        }

        text = GetComponent<Text> ();
        health = 0;
    }

    void Update(){
        health = gameControl.health;
        text.text = "Health: " + health;
    }
}

I made Change on value variable Health in GameControl. but Get Health always access 100 the same as first time when variable GameControl haven't changed. I think it was maybe because we use "new" on GetHealth class.

GameControl gameControl = new GameControl();

Is there another way to solve this without "new".

2
  • 1
    You need to make the classes static. I think that's what you want. Commented Dec 31, 2015 at 20:03
  • It should be noted that you can achieve your own value types with structs. Doing this you can init them with "MyStruct = 31". May not be what you're looking for, but it can be useful in the right situations. Commented Dec 31, 2015 at 20:49

2 Answers 2

1

If instantiation of player was in updateStat method and it is calling in void update() method, it cause to instantiate player object again and again. You can try this or instantiate playerclass2 in constructor. Also making health and experience static will help if only player object exist.

Class StatClass{
  PlayerClass playerclass2;
  public float health2;
  public float experience2;

  public void updateStat(){
     if(playerclass2==null) playerclass2 = new PlayerClass();
     health2 = playerclass2.health;
     experience2 = playerclas2.experience;
  }
}

EDIT

You can't use new keyword since it is derived from MonoBehavior. Have to use ClassName.method() format to call methods of derived classes from MonoBehavior

Sign up to request clarification or add additional context in comments.

1 Comment

Note that it should read "public" and not "publuc". Simple typo.
1

Thanks all, I have Found it.

It just have to

health = GameControl.control.health;

I don't have to instantiate it I just Call class and then initialize of class and then variable of class.

1 Comment

Well. You can't use new keyword since it is derived from mono behavior. Also you can set variables to public and drag and drop components in the inspector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.