Unity3d Unity脚本引用为null取决于使用引用的函数

Unity3d Unity脚本引用为null取决于使用引用的函数,unity3d,Unity3d,我制作了一个脚本,它引用了一些公共对象。 但取决于我调用的函数,一个引用是好的,但在另一个函数中它是空的,我不明白为什么 在函数onSave()中,对数据库的引用很好,我可以像add一样访问她的函数。 然后,当我调用函数edit(它应该将一个对象检索到数据库并用数据填充表单)时,对数据库的引用为null 错误日志(第33行是这一行:Step=this.database.get(this.scrollList.selectedItem);: 这是我的剧本: using UnityEngine; u

我制作了一个脚本,它引用了一些公共对象。 但取决于我调用的函数,一个引用是好的,但在另一个函数中它是空的,我不明白为什么

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

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

这是我的剧本:

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!");
    }
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类数据库:MonoBehavior{
//每帧调用一次更新
无效更新(){
}
私有列表步骤=新列表();
//用于初始化
无效开始(){
}
公开列表步骤{
得到{
返回此步骤;
}
}
公共void addStep(步骤newStep)
{
//如果(!this.exist(newStep.Uid))
this.steps.Add(newStep);
}
公共无效删除步骤(步骤新闻步骤)
{
此.steps.Remove(newStep);
}
public void updateStep(字符串uid,步骤newStep)
{
Step Step=this.Steps.Find(o=>o.Title==newStep.Title);
如果(步骤!=null){
步骤标题=新闻步骤标题;
step.Content=newStep.Content;
}
}
私有布尔存在(字符串uid)
{
if(this.Steps.Find(o=>o.Uid==Uid)!=null)
返回(真);
返回(假);
}
公共步骤获取(字符串uid)
{
Step Step=this.Steps.Find(o=>o.Uid==Uid);
Log(“获取数据库”);
Debug.Log(步骤标题);
返回(步骤);
}
公共内容(){
foreach(本步骤中的步骤。步骤){
Log(“#####调试步骤####”);
Debug.Log(step.Uid);
Debug.Log(步骤标题);
Debug.Log(步骤内容);
}
}
公共数据库(){
this.steps.Clear();
Log(“数据库已清除!”);
}
}

目前,我使用一个静态类而不是引用,它是这样工作的,但我确实想知道这个空引用的原因。^^。哪个引用是空的?您能显示准确的错误消息吗?另外,您何时调用这些函数?@SirBraneDamuj引用是StepForm类中的数据库字段。当调用edit(),但在调用onSave()时它会工作。这两个函数都是从button.onClick()调用的。它在哪里初始化的?谢谢:)您是否用
Debug.Log
验证了它确实是
数据库
的空值,或者它可能是
滚动列表
?这两个都在第33行中引用,因此任何一个都可以为null
onSave()
不引用
scrollList
。如果这是实际为null的值,它将解释为什么该值有效而
edit()
无效。
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!");
    }
}