簡體   English   中英

Unity C#如何更改其他類中的變量?

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

如何在另一個類中更改變量?

public class Manager : MonoBehaviour {

    public bool isDead = false;


    void Start () {


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

    }


    void Update () {



    }
}

我正在嘗試將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);
    }





}

我已經在debug.log中添加了顯示manager.isDead的值,並且打印為true,但是在檢查器中,當我運行它並單擊GameControl時,單擊包含Manager.cs的對象,它顯示isDead = false。 我不確定如何在GameControl Manager.cs中將isDead的值設置為true。

謝謝,我是Unity新手

在Health類中尚未設置manager屬性。

變更Manager manager; 進入Manager manager = new Manager();

您不是要從另一個類更改變量,而是要更改某個類的對象的屬性。

您的描述不清楚。 因此,這里是您嘗試做的兩種可能的變體:
1.場景中有兩個對象,第一個對象附加了Manager.cs,第二個對象附加了Health.cs。
在這種情況下的解決方案:
一種。 在Health.cs中修改Manager經理; -> 公共經理經理; (在這種情況下,當您選擇第二個對象時,您將能夠在檢查器中設置一個字段。
b。 選擇場景中的第二個對象,鎖定(按小鎖定圖標)檢查器
C。 選擇第一個對象並將其拖放到第二個對象的“管理器”字段中。

2.您有一個對象,並且兩個腳本都已附加到該對象。
解:
添加到Health.cs中的開始功能
void Start () { health = maxHealth; manager = gameObject.GetComponent<Manager>(); }

出現該錯誤的原因是,團結不知道此Manager是什么。
統一而言,一個腳本即使位於同一文件夾或游戲對象中也無法自動識別其他腳本。
為了訪問其他腳本,您需要為變量manager分配實際的腳本manager.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>();
}

然后,您應該能夠根據需要更改變量。 希望這可以幫助。

嘗試更改public bool isDead = false;

public static bool isDead = false;

在經理類中,並相應地更改其余部分

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM