- Home /
Tutorial for manual instantiation of prefabs yields null exception
I followed the guide on this page:
https://docs.unity3d.com/Manual/InstantiatingPrefabs.html
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?
Answer by Hellium · Mar 03, 2018 at 09:13 AM
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 !
I tried setting from Inspector as you suggested, but it changes from Bullet
to None (Bullet)
at runtime and continues to give the same null error. I also tried the code solution (as you can see in my edited version of my question above, with several other attempts) and it did not work either.
If the prefab goes null when you start the game, then, you may reassign it somewhere (in the Start function maybe, remove the Resources.Load
if you use the inspector) or your destroy it using Destroy( bulletPlaceholder )
Are you sure that you use a prefab? In other words did you drag a prefab from the project view to the variable or did you drag an instance from the scene or hierarchy view? You want to use a prefab. If your "rocket" object has some kind of autodestruct your referenced object may destroy itself. Prefabs are assets in the project which are inactive. When you instantiate them you will create a clone into the scene.
Answer by upasnavig90 · Mar 03, 2018 at 09:15 AM
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.
I tried setting from Inspector as you suggested, but it changes from Bullet
to None (Bullet)
at runtime and continues to give the same null error.
Ahh, I tried your code with changing line: public Rigidbody2D bulletPlaceholder; and assigning it from inspector, and put the following code in update function:
void Update()
{
if(Input.Get$$anonymous$$ey("up")) {
Rigidbody2D bulletClone = (Rigidbody2D)Instantiate(bulletPlaceholder);
}
}
it is working totally fine.
Your answer
Follow this Question
Related Questions
How can I create a prefab variant in C# script? 1 Answer
How to Instantiate a GameObject from a ScriptableObject piece of script? 0 Answers
Can't change change values of instantiated gameobject from another script 1 Answer
GameObject variable points to instance instead of prefab when prefab gets instantiated 2 Answers
Updating Pattern of Prefabs 1 Answer