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 bulletPlaceholder
in 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?