Unity脚本引用为null取决于哪个函数使用引用

时间:2015-04-15 13:21:36

标签: unity3d

我制作了一个采用公共对象引用的脚本。 但是根据我调用的函数,一个引用是正常的,但在另一个函数中它是空的,我不明白为什么。

在函数onSave()中,对数据库的引用很好,我可以像add一样访问她的函数。 然后,当我调用函数编辑(它应该将对象检索到数据库并用数据填充表单)时,对数据库的引用为空。

错误日志(第33行是这一行:Step step = this.database.get(this.scrollList.selectedItem);

> NullReferenceException: Object reference not set to an instance of an object
StepForm.edit () (at Assets/Prefabs/Scripts/StepForm.cs:33)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:110)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:574)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:716)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()

这是我的剧本:

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

public class StepForm : MonoBehaviour {

    public  Database    database;
    public  ScrollList  scrollList;
    public  InputField  title;
    public  InputField  content;
    private string      uid;

    // Use this for initialization
    void Start () {

    }

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

    }

    public  void    onSave(){
        Step newStep = new Step (this.title.text, this.content.text);
        this.database.addStep (newStep);
    }

    public  void    onCancel(){
        Debug.Log ("onCancel");
    }

    public  void    onDelete(){
        Debug.Log ("onDelete");
    }

    public  void    edit(){
        Step step = this.database.get(this.scrollList.selectedItem);

        if (step != null) {
            this.title.text = step.Title;
            this.content.text = step.Content;
        }
    }
}

脚本参考源:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Database : MonoBehaviour {

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

    }

    private List<Step> steps = new List<Step>();

    // Use this for initialization
    void Start () {

    }

    public List<Step> Steps {
        get {
            return this.steps;
        }
    }

    public void addStep(Step newStep)
    {
        //if (!this.exist(newStep.Uid))
            this.steps.Add(newStep);
    }

    public void deleteStep (Step newStep)
    {
        this.steps.Remove(newStep);
    }

    public void updateStep(string uid, Step newStep)
    {
        Step    step = this.Steps.Find(o => o.Title == newStep.Title);

        if (step != null) {
            step.Title = newStep.Title;
            step.Content = newStep.Content;
        }
    }

    private bool    exist(string uid)
    {
        if (this.Steps.Find (o => o.Uid == uid) != null)
            return (true);
        return (false);
    }

    public  Step    get(string uid)
    {
        Step step = this.Steps.Find (o => o.Uid == uid);

        Debug.Log ("GET DATABASE");
        Debug.Log (step.Title);
        return (step);
    }

    public  void    debugContent(){
        foreach (Step step in this.steps) {
            Debug.Log ("### Debug Step ###");
            Debug.Log(step.Uid);
            Debug.Log(step.Title);
            Debug.Log(step.Content);
        }
    }

    public  void    clearDatabase(){
        this.steps.Clear ();
        Debug.Log ("Database cleared!");
    }
}

0 个答案:

没有答案