简体   繁体   中英

Unity C# How do I change a variable from a different class?

How do I change the variable in another class?

public class Manager : MonoBehaviour {

    public bool isDead = false;


    void Start () {


        GameObject Slugbert = (GameObject)Instantiate(Resources.Load("Slugbert"));

    }


    void Update () {



    }
}

I'm trying to change the isDead boolean from false to true.

using UnityEngine;
using System.Collections;
using System;

public class Health : MonoBehaviour {






    public GameObject obj;

    Manager manager;

    public int maxHealth = 10;
    public int health = 10;
    public GameObject deathInstance = null;
    public Vector2 deathInstanceOffset = new Vector2(0,0);


    void Start () {
        manager = obj.GetComponent<Manager>();
        health = maxHealth;
    }

    void Update () {

        if (Input.GetKey ("up")) {

            Debug.Log ("Self Destruct Activated");
            TakeDamage();


        }

    }

    public void TakeDamage()
    {



        health -= 100;

        if (health <= 0)
        {


            OnKill();
            Debug.Log ("Running it");

        }
    }

    public void OnKill()
    {

        manager.isDead = true;
        Debug.Log(manager.isDead);


        if (deathInstance) {
            var pos = gameObject.transform.position;
            GameObject deathObject = Instantiate (deathInstance, new Vector3(pos.x + deathInstanceOffset.x, pos.y + deathInstanceOffset.y, pos.z), Quaternion.identity) as GameObject;
        }


        Destroy(gameObject);
    }





}

I've added in a debug.log showing the value of manager.isDead, and it's printing true, however in the inspector when I run it and click on GameControl, the object containing Manager.cs it shows that isDead = false. I'm not sure how I get the value of isDead in GameControl Manager.cs to equal true.

Thanks, I'm pretty new to Unity

The manager property is not set yet in the class Health.

Change Manager manager; into Manager manager = new Manager();

You are not changing a variable from another class but you are changing the property of an object of a certain class.

Your description is unclear. So here two possible variants of what you are trying to do:

Solution in this case:
in Health.cs modify Manager manager; -> public Manager manager; ( in that case you wil be able to se a field in inspector when you choose the second object.
b. Select the second object in scene, lock (press small lock icon) inspector
c. Select the first object and drag&drop it to the Manager field of the second object.


Solution:
add to Start function in Health.cs
void Start () { health = maxHealth; manager = gameObject.GetComponent<Manager>(); }

The reason you're having that error is because unity doesn't know what this Manager is.
In unity, a script cannot automatically recognize other scripts, even though it is located in the same folder or game object.
In order to access other scripts, you need to assign the variable manager with the actual script manager.cs . Here's how:

Health.cs

// This is the game object where manager.cs is attached to.
// Assign the game object from the inspector, drag drop the game object to this field.
// If manager.cs and health.cs is in the same game object, you don't need this.
public GameObject obj;
Manager manager;

void Start () {
    health = maxHealth;

    // This is where you actually assign manager variable
    // GetComponent<Manager> meaning you want to get a game object component with
    // type Manager, which is your script manager.cs
    // If manager.cs and health.cs is in the same game object, replace "obj" with
    // "gameObject"
    manager = obj.GetComponent<Manager>();
}

And then you should be able to change the variable like you wanted. Hope this helps.

try to change public bool isDead = false; to

public static bool isDead = false;

in Manager Class and change remaining accordingly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM