Object Pool object reference is null?

Hey.
So I’m new to the whole Unity thing and basically for a college assignment I have to assign a design pattern to my final year project. Since I’m doing a game in Unity I need to apply it to this. I was going to apply it to different spells that you can select to shoot different objects.

So I followed a YouTube tutorial on how to create a ObjectPool and I thought everything was going okay, however when I start the game, upon clicking, nothing emits, and I also get an error at the bottom saying “Object reference was not set to an instance of an object”.

I created a prefab with a sphere in it. I created 2 scripts, 1 called Object Pool, the other called Projectile. My Camera has its own script called FirstPersonController that is used for movement. In the ObjectPool script I detect if the user has pushed down the mouse button, then I call the ActivatedProjectile method.

EDIT

I get 2 types of error. The first appears only once at the start of the code:

ArgumentException: The thing you want to instantiate is null.
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineObject.cs:104)
UnityEngine.Object.Instantiate (UnityEngine.Object original) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineObject.cs:90)
ObjectPool.InstantiateProjectiles () (at Assets/Scripts/ObjectPool.cs:28)
ObjectPool.Start () (at Assets/Scripts/ObjectPool.cs:12)

The other appears multiple times as follows:

NullReferenceException: Object reference not set to an instance of an object
ObjectPool.ActivateProjectile () (at Assets/Scripts/ObjectPool.cs:37)
ObjectPool.Update () (at Assets/Scripts/ObjectPool.cs:20)

Here is the code I have for ObjectPool class.

using UnityEngine;
using System.Collections;

public class ObjectPool : MonoBehaviour {

	GameObject[] projectiles = null;
	public int numberOfProjectilesToCreate = 0;

	// Use this for initialization
	void Start () {
		projectiles = new GameObject[numberOfProjectilesToCreate];
		InstantiateProjectiles ();
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(Input.GetMouseButton(0))
		{
			ActivateProjectile();
		}
	}

	private void InstantiateProjectiles()
	{
		for (int i = 0; i < numberOfProjectilesToCreate; i++) 
		{
			projectiles *= Instantiate(Resources.Load("Prefabs/prefab_projectile")) as GameObject;*

_ projectiles*.SetActive(false);_
_
}_
_
}*_

* private void ActivateProjectile()*
* {*
* for (int i = 0; i < numberOfProjectilesToCreate; i++)*
* {*
_ if(projectiles*.activeSelf == false)
{
projectiles.SetActive(true);
projectiles.GetComponent().Activated();
}
}
}
}*_

Anybody have any ideas on what I’m doing wrong?

It would help if you gave us the line number of the exception.

–

What line is generating the null? Are you sure your Instantiate() is succeeding? Not your error, but it would be best to only do the Resources.Load() once in Start().

–

@stevethorne So sorry I completely forgot ha. I get errors on lines 12, 20 and 37 I believe. I updated the OP with the full error messages. @robertbu I updated the OP with the full error codes, my bad. I'm not too sure what is working and whats not, as I said I'm very new to the unity work flow. Ah okay I will change that now thanks

–

1 Answer

1

For Resources.Load(“Prefabs/prefab_projectile”) you need to have the folder “Prefabs” inside a folder named “Resources”.

Ah I'm an idiot I had an Asset folder but didn't have them inside a Resource folder. Thanks man :)

–