第一次使用Stackoverflow,非常感谢您提供的任何帮助或示例。目标是从另一个脚本中填充名为“playerList”的列表,其中包含“PlayerTank”对象。
我在名为“PlayerList”的脚本中创建了一个名为“playerList”的列表
在此过程中,我创建了子类“PlayerTank”及其构造函数。目标是使用“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"));
}
}
当然,以上所有方法都行不通!非常感谢您的帮助!
PlayerList' does not contain a definition for
Add' and no extension methodAdd' of type
PlayerList' could be found (are you missing a using directive or an assembly reference?)看起来它没有识别为列表。 - Jim