NullReferenceException: Object reference not set to an instance of an object
Hello,
I have a problem with object pooler function since i adding some rules for prefab spawning (min spawn & max spawn). When i run the code, It doesnt work and keeps prompt me errors: NullReferenceException: Object reference not set to an instance of an object objectpooler.GetPooledObject () (at Assets/Script/objectpooler.cs:66) spawncontroller.Update () (at Assets/Script/spawncontroller.cs:56)
Here's the code: (before this code, i have been trying many ways, such as build a function class for control the spawn rules and call it in "objectpooler" class but its also give me the same errors)
public GameObject GetPooledObject()
{
for (int i = 0; i < pooledObjects.Count; i++)
{
if (!pooledObjects[i].activeInHierarchy)
{
if (pooledObjects[i].GetComponent<PrefabStat>().max > globalvariables.freq[pooledObjects[i].GetComponent<PrefabStat>().index])
{
continue;
}
// globalvariables.freq[pooledObjects[i].GetComponent<PrefabStat>().index] += 1;
return pooledObjects[i];
}
}
do {
pooledObjectindex = Random.Range(0, pooledObject.Length);
// Debug.Log(pooledObjectindex);
// checkresult = check.freqcheck(pooledObjectindex);
// Debug.Log(checkresult);
// if (checkresult == true)
}
while (pooledObject[pooledObjectindex].GetComponent<PrefabStat>().max > globalvariables.freq[pooledObject[pooledObjectindex].GetComponent<PrefabStat>().index]);
GameObject obj = (GameObject)Instantiate(pooledObject[pooledObjectindex]);
obj.SetActive(false);
pooledObjects.Add(obj);
globalvariables.freq[pooledObject[pooledObjectindex].GetComponent<PrefabStat>().index] += 1;
return obj;
}
and the code i use for calling it
// Update is called once per frame
void Update()
{
Quaternion spawnRotation = Quaternion.identity;
if (transform.position.x < groundspawnpoint.position.x)
{
//new ground position
transform.position = new Vector3(transform.position.x + groundwidth + distancebetween, transform.position.y, transform.position.z);
//spawn ground
GameObject groundOBJ = theObjectPool.GetPooledObject();
groundOBJ.transform.position = transform.position;
groundOBJ.transform.rotation = transform.rotation;
groundOBJ.SetActive(true);
//ground width
groundwidth = groundOBJ.GetComponent<BoxCollider2D>().size.x;}}
It keeps happen, why i can't called some function or get some variables from others class in the function that i use to be called in others function
Thanks In Advance ^^, sorry about my english.