简体   繁体   English

Unity对象参考+序列化字段。 我想念什么?

[英]Unity object reference + serialized field. What am I missing?

I have a ui Button called "Attack Button". 我有一个名为“攻击按钮”的ui按钮。 I have a script called HeroBattleController attached to my Gameobject. 我的游戏对象上附加了一个名为HeroBattleController的脚本。 I have an attack button set as a serialized field and the attack button object dropped on the script in the editor. 我将攻击按钮设置为序列化字段,并且攻击按钮对象放在了编辑器中的脚本上。

[SerializeField]
private Button AttackButton;

public void SetButtonStatus(bool status) {
    AttackButton.interactable=status;
}

Trying to access that gives me the error NullReferenceException: Object reference not set to an instance of an object 尝试访问时出现错误NullReferenceException:对象引用未设置为对象的实例

I thought putting the object in the editor would allow me to access it without having to "Find" the object. 我认为将对象放在编辑器中将使我可以访问它,而不必“查找”该对象。 Can anyone point me in the right direction? 谁能指出我正确的方向?

在此处输入图片说明

The error is on the line 错误就在网上

AttackButton.interactable=status;

Complete HeroBattleController script 完整的HeroBattleController脚本

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class HeroBattleController: MonoBehaviour {

    public static string SelectedHero;

    [SerializeField]
    private Button AttackButton;

    public void SetButtonStatus(bool status) {
        AttackButton.interactable=status;

    }

    public void HeroTouch() {
        Debug.Log("Hero was touched: "+this.name);

        SetButtonStatus(false);
    }

    // Use this for initialization
    void Start() {

    }

    // Update is called once per frame
    void Update() {

    }
}

HeroTouch is called via onClick from the heroprefab object. 通过HeroPrefab对象的onClick调用HeroTouch。

Update: I had the herobattlecontroller script attached to two objects, the gameobject at the root of the scene and the heroprefab object. 更新:我将herobattlecontroller脚本附加到两个对象,场景根部的game对象和heroprefab对象。 I removed it from the gameobject and only have it on the prefab. 我从游戏对象中删除了它,只将其放在预制件上。 However now when I drag the attack button to the script section on the prefab it stays bold and when I run the game the attack button reference is gone. 但是现在,当我将“攻击”按钮拖动到预制板上的脚本部分时,它保持粗体,并且在我运行游戏时,“攻击”按钮引用消失了。 I can drag the button to the running object and it functions as I expected. 我可以将按钮拖动到正在运行的对象上,并且它可以按预期运行。 I'm clearly missing something on the connection between the object hierarchy and where the objects are usable. 我显然在对象层次结构和对象可用位置之间的连接上缺少某些内容。 Putting the hero battle controller script on the root gameobject only acts the same way meaning it's bold and when ran it's missing the link. 将英雄战斗控制器脚本放在根游戏对象上的方式仅相同,这意味着其为粗体,并且在运行时缺少链接。

hierarchy during edit 编辑期间的层次结构

在此处输入图片说明

hierarchy during runtime 运行时的层次结构

在此处输入图片说明

Does HeroBattleController attached to a prefab? HeroBattleController是否连接到预制件? and does the AttackButton is on the same prefab ? 并且AttackButton是否在同一预制件上? if not then there is a problem. 如果没有,那就有问题了。

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

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