静态列表在每次调用时实例化

时间:2018-12-03 13:24:34

标签: c# unity3d

我是C#编码和Unity的新手,使用列表时遇到问题。

我有一个静态类,其中保存供跨场景使用的数据,那里有一个GameObject列表。这是该代码:

 public static class data {
     private static List<GameObject> objectsInScene;

     static sendsize() {
         objectsInScene = new List<GameObject>();
     }

     public static List<GameObject> ObjectsInScene
     {
        get{
            return objectsInScene;
        }
     }

     public static void addObject(GameObject obj)
     {
         objectsInScene.Add(obj);
     }
 }

当我尝试访问列表时,即使我已经用GameObjects填充它,它也只是返回null。

还尝试将private static List<GameObject> objectsInScene;更改为private static List<GameObject> objectsInScene = new List<GameObject>();并仍然返回null

我正在此脚本中填充列表(使用检查器中的事件调用add函数)

   public class Add : MonoBehaviour {
        public void add(GameObject obj){
            GameObject objInstance = Instantiate(obj);
            sendsize.addObject(objInstance);
        }
    }

我正在尝试通过以下脚本访问它:

public class Setup3D : MonoBehaviour {
    void Start() {

        foreach (GameObject obj in data.ObjectsInScene) 
        {
            Instantiate(obj);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我们这里有一点XY Problem。这是列表为空的答案。

public static class Data
{
    public static List<GameObject> ObjectsInScene = new List<GameObject>();

    public static void AddObject(GameObject obj)
    {
        ObjectsInScene.Add(obj);
    }
}

public class Setup3D : MonoBehaviour
{
    public GameObject prefab;

    void Start()
    {
        // Adding objects to your list
        GameObject objInstance = Instantiate(prefab);
        Data.AddObject(objInstance);

        // cycling through the list
        foreach (GameObject obj in Data.ObjectsInScene)
        {
            Instantiate(obj);
        }
    }
}

别忘了将GameObject预制件拖到Unity中检查器中的GameObject插槽中。否则,您会得到NullReferenceException

这里的真正问题是OP希望将实例化的GameObjects保存在静态列表中,以便能够在另一个场景中再次实例化它们。您不能这样做,因为您正在将引用保存到GameObject。 相反,您应该做的是保存对象的变换数据,然后在另一个场景中重新实例化它们,然后应用变换。 像这样:

using System.Collections.Generic;
using UnityEngine;

public static class Data
{
    public static List<DataStructure> ObjectsInScene = new List<DataStructure>();

    public static void AddObject(GameObject obj)
    {
        ObjectsInScene.Add(new DataStructure
        {
            position = obj.transform.position,
            rotation = obj.transform.rotation,
            scale = obj.transform.localScale
        });
    }
}

public class DataStructure
{
    public Vector3 position;
    public Quaternion rotation;
    public Vector3 scale;    
}

public class Setup3D : MonoBehaviour
{
    public GameObject prefab;

    void Start()
    {
        // Adding objects to your list
        GameObject objInstance = Instantiate(prefab);
        Data.AddObject(objInstance);

        // cycling through the list
        foreach (DataStructure obj in Data.ObjectsInScene)
        {
            var instantiated = Instantiate(prefab);
            instantiated.transform.position = obj.position;
            instantiated.transform.rotation = obj.rotation;
            instantiated.transform.localScale = obj.scale;
        }
    }
}