Adding Instantiated Objects to ArrayList but it returns null C#

Hello,
I have an obstacles manager that keeps track of all obstacles in the scene, instantiating them and then supposed to destroy them. In order to do this I wanted to keep references to the Instantiated objects in an ArrayList (or List but I’m having the same issue in both).

When I add the Instantiated object to the ArrayList the index count goes up but when I try to access the index it returns null. I can see the objects in the scene so I don’t understand the problem.

Here is my code, I commented out one section but thats another route I was taking to see if it would solve the issue. Any help would be great.

public int maxObstacles, minObstacles;
	public float minCreateTime, maxCreateTime;//Randomize time to create objects between these 2 values
	private float createTime, currentTime; //Random amount of time between the creation of obstacles
	private Vector3 obstacleBounds;//The position for which to remove the obstacle from the scene list
	public Transform cameraTransform;
	public GameObject[] obstacleTypes;
	private Transform tempObstacle;
	private ArrayList obstaclesInSceneList;//Obstacles currently in the scene


// Use this for initialization
void Start () 
{
	obstaclesInSceneList = new ArrayList();
	createTime = Random.Range(minCreateTime, maxCreateTime);
	currentTime = 0;
}

// Update is called once per frame
void Update () 
{
	
	if(obstaclesInSceneList.Count <= minObstacles && obstaclesInSceneList.Count <= maxObstacles)
	{
		if(currentTime >= createTime)
		{
			createTime = Random.Range(minCreateTime, maxCreateTime);
			currentTime = 0;
			

			tempObstacle = Instantiate(obstacleTypes[Random.Range(0, obstacleTypes.GetLength(0) - 1)],
				new Vector3(Random.Range(-(Screen.width /2), (Screen.width /2)),cameraTransform.position.y - Screen.height, 0),
				transform.rotation) as Transform;
			Debug.Log("Temp is null" + tempObstacle == null);
			
			obstaclesInSceneList.Add(tempObstacle);

            /*obstaclesInSceneList.Add(Instantiate(obstacleTypes[Random.Range(0, obstacleTypes.GetLength(0) - 1)],
				new Vector3(Random.Range(-(Screen.width /2), (Screen.width /2)),cameraTransform.position.y - Screen.height, 0),
				transform.rotation) as Transform);*/
		}
	}

                                //Check to see if any of the obstacles are off screen above the camera

	obstacleBounds = new Vector3(0, cameraTransform.position.y + Screen.height, 0); 
	
	Debug.Log(obstaclesInSceneList.Count);
	try
	{
		for(int i = 0; i < obstaclesInSceneList.Count; i++)
		{
			
			if(obstaclesInSceneList*.position.y > obstacleBounds.y)*
  •   		{*
    

_ obstaclesInSceneList.Remove(obstaclesInSceneList*);*_

* }*
* } *
* }*
* catch*
* {*
* Debug.Log(“Something is null”); *
* }*
* currentTime +=1;*

* }*

Problem in removing? ‘Remove’ will change the Count, so the indexes will be off. Use ‘foreach’ instead of ‘for’

You stepped into the as-cast trap…

You declared your obstacleTypes array as GameObject array. That’s not a problem, but when you instantiate you try to cast the instantiated object to Transform which of course fails since a GameObject isn’t a Transform. Because you used the as-cast you don’t get any casting error. The cast just returns null.

Btw. Why do you use an ArrayList? It’s slow and requires casting since it’s not strongly typed. Use a generic List<> instead. First you should decide if you want to store GameObject references or Transform references and adjust your obstacleTypes array and the cast.

The generic List requires the System.Collections.Generic namespace.

1.create ArrayList for numbers of obstacles that’s you want instantiate .
2.Use a Switch case for different position .

var obstacles : GameObject;

private var delta_time : int;

private var stopTime : float = 0;

private var scene_load_time : float = 0.0;
private var loop_counter : int = 0;

private var caseCalled : int = 0;

var pos1 : Vector3;
var pos2 : Vector3;
var pos3 : Vector3;
var pos4 : Vector3;

function Start ()
{
CaseCalled();
}

function Update ()
{
if(stopTime > delta_time)
{
var temp_time : int = scene_load_time/30;
if(temp_time == loop_counter)
{
loop_counter += 1;
if(loop_counter > 6)
{
loop_counter = 6;
}
}

for(var i =0; i< loop_counter ; i++)
{
CaseCalled();
}
stopTime = 0;
}
stopTime += Time.deltaTime;
scene_load_time += Time.deltaTime;

}

pos1 = Vector3(Random.Range(-500,500),400,0);
pos2 = Vector3(-500,Random.Range(-360,400),0);
pos3 = Vector3(Random.Range(-500,500),-360,0);
pos4 = Vector3(500,Random.Range(-360,400),0);

switch(caseCalled)
{
	case 0 :  
			Instantiate(obstacles[Random.Range(0,7)],pos4,Quaternion.EulerAngles(-89.5,0,0));
			break;
	case 1 : 
			Instantiate(obstacles[Random.Range(0,7)],pos1,Quaternion.EulerAngles(-89.5,0,0));
			break;
	case 2 : 
			Instantiate(obstacles[Random.Range(0,7)],pos2,Quaternion.EulerAngles(-89.5,0,0));
			break;
	case 3 : 
			Instantiate(obstacles[Random.Range(0,7)],pos3,Quaternion.EulerAngles(-89.5,0,0));
			break;
	default : 
			Instantiate(obstacles[Random.Range(0,7)],pos3,Quaternion.EulerAngles(-89.5,0,0));
}

}