Search Unity

Object reference not set to an instance of an Object/ Object Pooling

Discussion in 'Scripting' started by adamb93, Dec 25, 2017.

  1. adamb93

    adamb93

    Joined:
    Dec 17, 2017
    Posts:
    3
    Hi Guys,

    I am having issues with my object pooling script right now because of two errors detected in the Unity Console. The 1st error is a MissingReferenceException:

    " MissingReferenceException: The variable enemyPrefab of EnemyPool doesn't exist anymore.
    You probably need to reassign the enemyPrefab variable of the 'EnemyPool' script in the inspector.
    UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineObjectBindings.gen.cs:52)
    UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:155)
    UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:205)
    EnemyPool.Start () (at Assets/EnemyPool.cs:24)"


    The second issue is an Object reference not set to an instance of an Object:

    NullReferenceException: Object reference not set to an instance of an object
    EnemyPool.Update () (at Assets/EnemyPool.cs:36)


    The script is pasted below. Any help would be appreciated! Thank You!.



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

    public class EnemyPool : MonoBehaviour {
    public int enemyPoolSize = 10;
    public GameObject enemyPrefab;
    public float spawnRate = 4f;
    public float enemyMin = 1f;
    public float enemyMax = 3.5f;
    private float spawnXPosition = 10f;
    private int currentEnemy = 0;

    private GameObject[] enemies;
    private Vector2 objectPoolPosition = new Vector2(-15f, -25f);
    private float timeSinceLastSpawned;

    // Use this for initialization
    void Start () {

    enemies = new GameObject[enemyPoolSize];
    for (int i = 0; i < enemyPoolSize; i++)
    {
    enemies = (GameObject)Instantiate(enemyPrefab, objectPoolPosition, Quaternion.identity);
    }

    }

    // Update is called once per frame
    void Update () {
    timeSinceLastSpawned += Time.deltaTime;
    if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
    {
    timeSinceLastSpawned = 0;
    float spawnYPosition = Random.Range(enemyMin, enemyMax);
    enemies[currentEnemy].transform.position = new Vector2(spawnXPosition, spawnYPosition);
    currentEnemy++;
    if (currentEnemy >= enemyPoolSize)
    {
    currentEnemy = 0;
    }
    }

    }
    }"
     
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Hey, please use tags when posting code: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Anyway, you're not assigning your "enemies" array correctly. Compare this to the code in your Start method;
    Code (CSharp):
    1. enemies[i] = (GameObject)Instantiate(enemyPrefab, objectPoolPosition, Quaternion.identity);