Denni S
Denni S

Reputation: 134

C# UNITY Instantiate Class (Create Object) without using new operator

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".

Upvotes: 0

Views: 3721

Answers (2)

Lahiru Jayakody
Lahiru Jayakody

Reputation: 5400

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

Upvotes: 1

Denni S
Denni S

Reputation: 134

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.

Upvotes: 1

Related Questions