【问题标题】:Unity C# How do I change a variable from a different class?Unity C# 如何更改来自不同类的变量?
【发布时间】:2014-10-11 12:02:00
【问题描述】:

如何更改另一个类中的变量?

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 的新手

【问题讨论】:

  • @O.R.Mapper 并非每个NullReferenceException 都有相同的场景。
  • @Shaharyar:当manager.isDead = true;线上有NullReferenceException时,只有一种可能的情况,即managernull。 OP 似乎没有得出这个结论,所以他们的问题完全归结为我链接的问题 - “这是什么意思,我能做些什么?”
  • @O.R.Mapper 我的意思是我们应该指出导致异常的实际问题。

标签: c# unity3d


【解决方案1】:

您出现该错误的原因是因为 unity 不知道 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>();
}

然后您应该能够根据需要更改变量。希望这会有所帮助。

【讨论】:

  • 没问题 :) 我只是想帮忙
【解决方案2】:

尚未在 Health 类中设置 manager 属性。

Manager manager; 更改为Manager manager = new Manager();

您不是在更改另一个类的变量,而是在更改某个类的对象的属性。

【讨论】:

  • 这是修复错误但不会改变变量
  • 更改变量是什么意思。类 Health 的对象有一个属性管理器,它是类 Manager 的对象。通过此修改,您现在可以设置该属性的 isDead 属性。
  • 我该怎么做呢?
【解决方案3】:

您的描述不清楚。所以这里有两种可能的变体:
1.您在场景中有两个对象,第一个对象附加 Manager.cs,第二个对象附加 Health.cs。
这种情况下的解决方法:
a.在Health.cs中修改Manager manager; -> public 经理经理; (在这种情况下,当您选择第二个对象时,您将能够在检查器中设置一个字段。
b.选择场景中的第二个对象,锁定(按小锁图标)检查器
c. 选择第一个对象并将其拖放到第二个对象的管理器字段中。

2.您有一个对象和两个脚本。
解决方案:
添加到 Health.cs 中的 Start 函数
void Start () { health = maxHealth; manager = gameObject.GetComponent<Manager>(); }

【讨论】:

    【解决方案4】:

    尝试改变 public bool isDead = false;

    public static bool isDead = false;
    

    在经理类中并相应地更改剩余

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-19
      • 2017-01-08
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      相关资源
      最近更新 更多