Array or List, Index out of range, I've tried everything.. any other way of passing a list of values of an instantiated prefab to another script

So basically, I’ve set the List (tried with Array) filled it, works beautifully, but then when I try to either copy the whats inside the List into another script, or another function within the same script, no does the List act as though it is set, I always get an error, out of range, I tried approaching this from so many angles but still no success… :confused: Ultimately I’m just trying to find a way to pass the position of the instances I create from the prefab into the script of another prefab so that the AI code can track it, so unless there’s a better way, this is the attempt I’ve got, any help is greatly appreciated.

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

public class DotSpawner : MonoBehaviour {

float DotSpawnerTimer = 0f;
float BlobSpawnerTimer = 0f;

public GameObject Dots;
public GameObject Enemy;

int MaxBlobs = 0;

int MaxDots = 100;
public List<GameObject> Dot;

public GameObject Empty;

// Use this for initialization
void Start () {
    Empty = new GameObject();
    Dot = new List<GameObject>();
    for (int a = 0; a < MaxDots; a++)
    {
        Dot.Add(Empty);
    }
}

// Update is called once per frame
void FixedUpdate () 
{
    DotSpawnerTimer -= Time.deltaTime;
    if (DotSpawnerTimer <= 0)
    {
        CreateDots();
    }

    BlobSpawnerTimer -= Time.deltaTime;
    if (BlobSpawnerTimer <= 0)
    {

        Instantiate(Enemy, new Vector3(Random.Range(-1.85f, 1.24f), Random.Range(0.1f, 2.41f), 0f), Quaternion.identity);

        BlobSpawnerTimer = Random.Range(4f, 8f);
    }
}

void CreateDots()
{
    for (int a = 0, b = 0; a < MaxDots; a++)
    {
        if (Dot[a] == Empty || Dot[a] == null)  // <=-------  Accessing the list here works fine, but not in the function below, why?

        {
            GameObject DotCopy = Instantiate(Dots, new Vector3(Random.Range(-1.85f, 1.24f), Random.Range(0.1f, 2.41f), 0f), Quaternion.identity) as GameObject;
            Dot[a] = DotCopy;
            b++;
        }
        if (b >= 10)
        {
            DotSpawnerTimer = Random.Range(2f, 8f);
            break;
        }
    }
}

public Vector3 Result;

public Vector3 GetDotLocation(Vector3 Blob)
{
    for (int a = 0; a < MaxDots; a++)
    {         
        if (Dot[a] != Empty || Dot[a] != null)  // <=--------- This is where I get the error, out of range
        {
            Result.x = Dot[a].transform.position.x - Blob.x;
            Result.y = Dot[a].transform.position.y - Blob.y;
            Result.z = a;
            break;
        }
    }
    return Result;
}

bool DotStillActive;

public bool CheckDot(int DotIndex)
{
    if (Dot[DotIndex] == null)
    {
        DotStillActive = false;
    }
    else if (Dot[DotIndex] != null)
    {
        DotStillActive = true;
    }
    return DotStillActive;
}

}

Thank you for your time

What calls GetDotLocation()? I assume it’s being called from some other script? I’d guess it’s being called before the Start() method (above) initializes your Dot list. If that’s the case, you’ll need to juggle some stuff around to ensure your list is initialized before GetDotLocation is called.

You could verify that it’s a call order issue by placing a Debug.Log in the above Start() method as well as in the GetDotLocation() method. If you see the message from GetDotLocation() before the one in Start(), that’s the issue.

Depending on where/when/how you’re calling GetDotLocation(), the solution could be as simple as moving your Dot list initialization code from Start() to Awake().