Tutorial for manual instantiation of prefabs yields null exception

I followed the guide on this page:

Under the heading:

Instantiating rockets & explosions

And it is giving me the error:

ArgumentException: The Object you want to instantiate is null.

I used a slight modification of the code given in C#:

private Rigidbody2D bulletPlaceholder;

void fire() {
	if(Input.GetKey("up")) {
		Rigidbody2D bulletClone = (Rigidbody2D)Instantiate(bulletPlaceholder);
	}
}

Yes, I do see that bulletPlaceholderin this case is null, but this is how the suggested code in the Unity docs shows it being done, and I see no example of another way to do it, except for in the Answers forums. However, these suggestions also result in a null exception. For example:

Bullet bullet = (Bullet) Instantiate(Resources.Load("Bullets"));

Also tried as GameObject and GameObject bulletClone and Rigidbody2D bulletClone etc etc etc, and none of them work.

Have now also tried this:

private GameObject bulletPrefab;

void Start () {
	bulletPrefab = (GameObject)Resources.Load("/Prefabs/Bullets");
}

void Update () {
	fire();
}

void fire() {
	if(Input.GetKey("up")) {
		// how can it possibly say bulletPrefab is null if it was defined in Start()?
		GameObject bullet = (GameObject)Instantiate(bulletPrefab);
	}
}

As an alternative, since I’ve spent 2 days trying to get this infuriating Instantiate(…) method to work, is there an alternative that is easier to use, or a more correct tutorial to follow?

As you suspect, bulletPlaceholder must not be null if you want to create a copy of it.

There are several possibilities:

Resources

Put the a gameobject called Bullet in a folder called Resources in your project and call:

private GameObject bulletPlaceholder ;

void Start()
{
     bulletPlaceholder  = (GameObject) Resources.Load("Bullet") ;
}

// ....
GameObject bullet = Instantiate( bulletPlaceholder );

Inspector

Change the visibility of your prefab and set it to public (or add the [SerializeField] attribute above the private variable)
Then, in the inspector, you will see a field called `` . Simply drag & drop a gameobject from your Project tab into this field, and you are ready to go !

hey, you have to assign “bulletPlaceholder” either from code by using GameObject.find(“someName”) or make it public variable and assign it from inspector.

just think, if you will not tell what is to instantiate, how unity will come to know what is in “bulletPlaceholder”. so you have to tell what “bulletPlaceholder” is.