How to get position of an object in a list
I'm new to Unity and C# (and OOP in general) and I'm having difficulty working with lists.
In the following code I'm successfully instantiating new objects and adding them to the list. Then when I try to output the position to Debug.Log I get an error: "Object reference not set to an instance of an object". It compiles and runs fine until it gets to the Debug.Log line. How can I grab a specific instance of an object from a list by that object's index from within the list?
Thank you!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameController : MonoBehaviour {
public GameObject unitToSpawn;
private List<Tank> TankList = new List<Tank>();
public Tank tank;
public int numTanks;
void Start () {
for (int i = 0; i < numTanks; i++) {
Vector3 spawnPosition = new Vector3 (Random.Range (-5, 5), 1, Random.Range (-5, 5));
Quaternion spawnRotation = Quaternion.identity;
TankList.Add (Instantiate (unitToSpawn, spawnPosition, spawnRotation) as Tank);
}
for (int i = 0; i < numTanks; i++){
Debug.Log ("i = " + i + " : " + TankList[i].transform.position);
}
}
}
Answer by MountainDrew · Aug 29, 2015 at 12:09 AM
So I ended up doing something similar to the way a list of Enemy objects is made in the 2D Roguelike Tutorial. In case anyone else comes across this problem here is the solution I got to:
GameController.cs:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameController : MonoBehaviour {
public static GameController instance = null;
public GameObject tankPreFab;
public int numTanks;
private List<Tank> tankList;
void Awake (){
instance = this;
tankList = new List<Tank>();
}
void Start () {
for (int i = 0; i < numTanks; i++) {
Vector3 spawnPosition = new Vector3 (Random.Range (-5, 5), 1, Random.Range (-5, 5));
Quaternion spawnRotation = Quaternion.identity;
Instantiate(tankPreFab, spawnPosition, spawnRotation);
}
for (int i = 0; i < numTanks; i++){
tankList[i].ID = i;
Debug.Log ("i = " + i + " : " + tankList[i].transform.position);
}
}
public void AddTankToList(Tank script)
{
tankList.Add(script);
}
}
And here is Tank.cs (which is attached to the Tank Prefab)
using UnityEngine;
using System.Collections;
public class Tank : MonoBehaviour {
public int ID; //unique ID for each tank
void Awake(){
GameController.instance.AddTankToList (this);
}
}
And now I have a list of tanks!
Answer by Vice_Versa · Aug 27, 2015 at 07:48 AM
this is not a problem with Getting the position in the list, you are doing that part correctly. Your not setting a value to Tank in the Start function, So when you make these new objects as type Tank, your giving them unknown values. instead of doing it "as Tank" do it "as GameObject" or "as Transform" and it should work
Thanks for your reply. So it works if I change the List declaration to
private List<GameObject> TankList = new List<GameObject>();
and then do set as GameObject
but now my list is a list of GameObjects ins$$anonymous$$d of a list of Tanks. Is there a way to do it as a list of tanks? Here is my Tank.cs file:
using UnityEngine;
using System.Collections;
public class Tank : $$anonymous$$onoBehaviour {
public int ID; //unique ID for each tank
}
Is there something I can do so that I can have a list of objects of the Tank class? I was hoping that because Tank inherits from $$anonymous$$onoBehaviour that it would also have Transform and all that good stuff. Again, I'm new to OOP so I could be misunderstanding how to appropriately use classes.
Thanks!
you need to make a seperate script called Tank, and give it whatever properties Tank needs, then at the top of this script do "using Tank;"
Your answer
Follow this Question
Related Questions
How to know if a list contains GameObjects with a specific Varaible values ? 0 Answers
How do I modify variables from a different class that belongs to the same C# file? 1 Answer
List resulting in out of range 0 Answers
Find the closest Enemy that has a tag added in a list? 1 Answer
Inventory slots number are wrong 0 Answers