C# Unity C如何更改不同类中的变量?
如何在另一个类中更改变量C# Unity C如何更改不同类中的变量?,c#,unity3d,C#,Unity3d,如何在另一个类中更改变量 public class Manager : MonoBehaviour { public bool isDead = false; void Start () { GameObject Slugbert = (GameObject)Instantiate(Resources.Load("Slugbert")); } void Update () { } } 我正在尝试将isDead布尔值从
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,但是在inspector中,当我运行它并单击GameControl时,包含manager.cs的对象显示isDead=false。我不确定如何使GameControl Manager.cs中的isDead值等于true
谢谢,我是Unity的新手尚未在类Health中设置manager属性 变更经理;进入经理=新经理
您不是在更改另一个类的变量,而是在更改某个类的对象的属性。您的描述不清楚。因此,这里有两种可能的变体: 1.场景中有两个对象,第一个对象附加了Manager.cs,第二个对象附加了Health.cs。 这种情况下的解决方案: A.在Health.cs中修改管理器;->公共经理;在这种情况下,当您选择第二个对象时,您将能够在inspector中使用一个字段。 B选择场景中的第二个对象,锁定按小锁定图标检查器 C选择第一个对象并将其拖放到第二个对象的管理器字段中。 2.您有一个对象和附加到它的两个脚本。 解决方案: 在Health.cs中添加启动函数 无效启动{ 健康=最大健康; manager=gameObject.GetComponent;
}您之所以会出现此错误,是因为unity不知道此经理是什么。 在unity中,脚本无法自动识别其他脚本,即使它位于同一文件夹或游戏对象中。 为了访问其他脚本,您需要为变量管理器分配实际的script manager.cs。以下是方法: Health.cs 然后你应该能够改变你想要的变量。希望这有帮助。尝试改变 公共bool isDead=false; 到
在Manager类中,并相应地更改剩余值可能重复的@O.R.Mapper并非每个NullReferenceException都具有相同的场景。@Shaharyar:当行Manager.isDead=true上存在NullReferenceException时;,只有一种可能的情况,即manager为null。OP似乎没有得出那个结论,所以他们的问题完全归结为我联系的问题-这是什么意思,我能做些什么?@O.R.Mapper我的意思是我们应该指出导致异常的实际问题。这是修复错误,但不是更改变量。更改变量是什么意思。类健康的对象有一个属性管理器,它是类管理器的对象。通过此修改,您现在可以设置属性isDead of this property。但是我该怎么做呢?没问题:我只是想帮您
// 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 static bool isDead = false;