简体   繁体   English

Unity C#如何更改其他类中的变量?

[英]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. 我正在尝试将isDead布尔值从false更改为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. 我已经在debug.log中添加了显示manager.isDead的值,并且打印为true,但是在检查器中,当我运行它并单击GameControl时,单击包含Manager.cs的对象,它显示isDead = false。 I'm not sure how I get the value of isDead in GameControl Manager.cs to equal true. 我不确定如何在GameControl Manager.cs中将isDead的值设置为true。

Thanks, I'm pretty new to Unity 谢谢,我是Unity新手

The manager property is not set yet in the class Health. 在Health类中尚未设置manager属性。

Change Manager manager; 变更Manager manager; into Manager manager = new Manager(); 进入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: 因此,这里是您尝试做的两种可能的变体:
1. You have two objects in scene and on the first you attached Manager.cs and on the second you attached Health.cs. 1.场景中有两个对象,第一个对象附加了Manager.cs,第二个对象附加了Health.cs。
Solution in this case: 在这种情况下的解决方案:
a. 一种。 in Health.cs modify Manager manager; 在Health.cs中修改Manager经理; -> public Manager manager; -> 公共经理经理; ( in that case you wil be able to se a field in inspector when you choose the second object. (在这种情况下,当您选择第二个对象时,您将能够在检查器中设置一个字段。
b. b。 Select the second object in scene, lock (press small lock icon) inspector 选择场景中的第二个对象,锁定(按小锁定图标)检查器
c. C。 Select the first object and drag&drop it to the Manager field of the second object. 选择第一个对象并将其拖放到第二个对象的“管理器”字段中。

2. You have one object and both scripts attached to it. 2.您有一个对象,并且两个脚本都已附加到该对象。
Solution: 解:
add to Start function in Health.cs 添加到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. 出现该错误的原因是,团结不知道此Manager是什么。
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 . 为了访问其他脚本,您需要为变量manager分配实际的脚本manager.cs Here's how: 这是如何做:

Health.cs 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; 尝试更改public bool isDead = false; to

public static bool isDead = false;

in Manager Class and change remaining accordingly 在经理类中,并相应地更改其余部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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