• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by stevenkitzes · Mar 03, 2018 at 08:44 AM · prefabsinstantiate prefabinstantiation

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stevenkitzes · Mar 03, 2018 at 09:24 AM 0
Share

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.

avatar image upasnavig90 stevenkitzes · Mar 03, 2018 at 09:32 AM 0
Share

are you destroying it somewhere??

avatar image upasnavig90 upasnavig90 · Mar 03, 2018 at 09:36 AM 1
Share

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.

avatar image
2

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 !

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stevenkitzes · Mar 03, 2018 at 09:25 AM 0
Share

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.

avatar image Hellium stevenkitzes · Mar 03, 2018 at 09:43 AM 2
Share

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 )

avatar image Bunny83 stevenkitzes · Mar 03, 2018 at 10:59 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

80 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I create a prefab variant in C# script? 1 Answer

Destroying instantiated prefabs destroy all of my instantiated prefabs 1 Answer

Unity Instantiates GameObjects in a wrong order 2 Answers

Is it necessary to assign the result of Instantiate() to a variable and is there a need to always typecast? 1 Answer

Need help understanding scripted prefab behavior - when I click one, the script runs on ALL prefab instances, not just the one I clicked. 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges