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… 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