将数据添加到统一c#中的另一个脚本的列表中

时间:2015-03-12 21:20:59

标签: c# unity3d

首次使用Stackoverflow,非常感谢您提供任何帮助或示例。目的是用另一个脚本中的'PlayerTank'对象填充名为'playerList'的列表。

我在名为'PlayerList'的脚本中创建了一个名为'playerList'的列表

在这里我还用构造函数创建了子类'LayerTank'。目的是用'PlayerTank'对象填充'playerList'。这是代码。

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

public class PlayerList : MonoBehaviour {

    public List<PlayerTank> playerList;

    public class PlayerTank { // Here is my class for PlayerTank

        public int playerNumber;
        public int playerPadNumber;
        public string playerName;
        public string playerColour;


        // This is a constructor to make an instance of my class. 

        public PlayerTank (int newPlayerNumber, int newPlayerPadNumber, string newPlayerName, string newPlayerColour) 
        {
            playerNumber = newPlayerNumber;
            playerPadNumber = newPlayerPadNumber;
            playerName = newPlayerName;
            playerColour = newPlayerColour;
        }


    }


void Start () {

        playerList = new List<PlayerTank>();

    }

    }
}

上述工作正常,花花公子。

但是,在一个名为“Test”的完全独立的脚本中,我现在尝试将playerTank对象添加到此列表中。

以下所有内容都没有接近工作,但我将其包含在内,以便对我的思维过程有所了解!

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


public class Test : MonoBehaviour {

    public GameObject playerData;
    PlayerList playerList;
    PlayerTank playerTank;



    // Use this for initialization
    void Start () {
        playerList = playerData.GetComponent<PlayerList> ();
        playerTank = PlayerList.PlayerTank;

        playerList.playerList.Add (new playerTank.playerTank(1,2,"a player name", "black")); 
    }

}

当然以上都没有作品!非常感谢您的期待!

1 个答案:

答案 0 :(得分:3)

首先欢迎使用StackOverflow。为了做你想做的事,你需要将这些脚本附加到层次结构中的游戏对象(最好是两个)。有两种解决方案:第一种是将PlayerList声明为Test的公共字段并通过UI分配(将包含PlayerList脚本的GameObject拖放到字段定义中) Test GameObject:

enter image description here

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


public class Test : MonoBehaviour {

    public PlayerList playerList; //replace GameObject with PlayerList 

    // Use this for initialization
    void Start () {

        var playerTankList = playerList.PlayerTankList; // You can access it directly

        playerTankList .Add (new PlayerList.PlayerTank(1,2,"a player name", "black")); 
    }

}

请记住将属性[Serializable]添加到类PlayerList中,以查看UI中Test中声明的字段。

第二种解决方案更直接。您只需使用Unity函数来检索与层次结构中存在的GameObject关联的特定类型。该函数名为FindObjectOfType();。第二个解决方案中的Test类看起来像这样:

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


public class Test : MonoBehaviour
{ 
    // Use this for initialization
    void Start()
    {
        var playerList = FindObjectOfType<PlayerList>();

        playerList.PlayerTankList.Add(new PlayerList.PlayerTank(1, 2, "a player name", "black"));
    }

}

编辑:

我在下面重写并添加了原始的PlayerList类。我用Awake改变了Start函数,在Start之前在Unity事件链中调用它。您仍然看到NullReferenceException的原因是因为未按任何特定顺序调用Start事件。因此,Test类中的Start事件可能发生在PlayerList类中的Start事件之前。但是我测试了它,如果你正确地关联GameObjects它可以工作:

PlayerList gameobject correctly set

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

[Serializable]
public class PlayerList : MonoBehaviour
{
    public List<PlayerTank> PlayerTankList;

    public class PlayerTank
    { // Here is my class for PlayerTank

        public int playerNumber;
        public int playerPadNumber;
        public string playerName;
        public string playerColour;


        // This is a constructor to make an instance of my class. 

        public PlayerTank(int newPlayerNumber, int newPlayerPadNumber, string newPlayerName, string newPlayerColour)
        {
            playerNumber = newPlayerNumber;
            playerPadNumber = newPlayerPadNumber;
            playerName = newPlayerName;
            playerColour = newPlayerColour;
        }
    }


    void Awake()
    {
        PlayerTankList = new List<PlayerTank>();
    }

}
希望它有所帮助。

相关问题