- Home /
How to adress a Transform inside a arraylist that's inside a arraylist.
I am trying to put a arraylist inside a arraylist and then put transforms inside the arraylist that is already inside the first arraylist. I tried the following. The debug I put in there returns a Null item but if I look at the count it says 7.
public class Main : MonoBehaviour {
private List<List<Transform>> playPieceList = new List<List<Transform>>();
void PieceSpawner()
{
List<Transform> playPieceListInside = new List<Transform>();
playPieceList.Add(playPieceListInside);
for(int e = 0; e < 7; e++)
{
Transform tempPlayPiece;
Vector3 posPiece = new Vector3(0.1708656f - 0.0574332f * e, 0.1f, -0.228953f);
tempPlayPiece = Instantiate (playPiece, posPiece, Quaternion.identity) as Transform;
playPieceList[0].Add(tempPlayPiece);
}
playPieceList.Add(playPieceListInside);
Debug.Log(playPieceList[0][0]);
}
}
Answer by GameVortex · Jan 30, 2014 at 08:44 AM
The debug returning a null means that the content of position 0 in the list inside the master list is null. This is most likely a result from the Instantiate going wrong. Put a debug to find out if the for loop actually instantiates objects and that the object is valid. Maybe the prefab reference is null or you could try instantiating it as a GameObject first and adding its transform to the list.
Another thing is that you are adding the list two times (at line 6 and 14) to the master list, you probably want to add it just once.
Also, don't say ArrayList when you mean List. =P
Thank you so much for your help! It was indeed the instantiate. It was a really stupid rookie mistake, but in the playpiece variable was a scene GameObject ins$$anonymous$$d of the Prefab. Also my appologies for adressing it as a Arraylist ins$$anonymous$$d of List. To be honest I didn't learn the difference yet. Anyway it works now thank you!
@gangafinti - if this answer answered your question, please click on the checkmark next to the left of the answer to close it out. Thanks.