Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
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.

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.

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.

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

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


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