Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by RoloJo · Mar 13, 2013 at 03:36 PM · instantiateprefabnull

ArgumentException: The prefab you want to instantiate is null.

I get the above error when implementing the following code. I have cut it down to what I think are the essential bits of code to make it easier to read. If you need more please let me know.

 public GameObject WolfObject;//prefab has been dragged into this object
 public GameObject SpeciesInstantiate(string SpeciesName,int IDNameNumber)
     {
 
             var NewGameObject=GameObject.Instantiate(WolfObject,Position,Quaternion.identity);
             NewGameObject.name=(SpeciesName+IDNameNumber.ToString());return (GameObject)NewGameObject;}


 }


That is called as follows:

 public class EnvironmentalEngine : MonoBehaviour 
 {
 GameObject ComObj;
 void Start () 
     {
         ComObj=GameSpecies.SpeciesInstantiate(SpeciesTag[j],ObjectLibrary.WorldSpeciesCount[j]);    }
 

Thanks in advance for any help :)

Comment
Add comment · Show 5
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 MD_Reptile · Mar 13, 2013 at 04:01 PM 0
Share

Perhaps the prefab got changed externally, like rena$$anonymous$$g in windows explorer, thus breaking the prefab connection? I really am guessing but just maybe re-assign the prefab? Probably won't help :p

avatar image RoloJo · Mar 13, 2013 at 04:07 PM 0
Share

Thanks, tried that now and it didnt help :P

avatar image Loius · Mar 13, 2013 at 04:17 PM 0
Share

In my experience, Unity functions in C# can be a lot more fussy about types at times when it might seem to make less sense to do so. Have you tried casting WolfObject to Object? Instantiate takes Object as its spawn argument and it may be having issues converting the type automagically.

avatar image RoloJo · Mar 13, 2013 at 04:21 PM 0
Share

Tried casting it to UnityEngine.Object. That didnt work. I appreciate the help though :)

avatar image robertbu · Mar 13, 2013 at 04:26 PM 1
Share

Have you tried Googling for 'The prefab you want to instantiate is null Unity3'? You will find some hits on UnityAnswers. $$anonymous$$any are uninitialized game objects, but there were several where the problem was more subtle. With the limited amount of code you gave here, it is unclear which (if any) of the more subtle issues might apply to your problem.

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by RoloJo · Mar 13, 2013 at 04:52 PM

Thanks for all the responses. After trawling the net I have found what makes it work:

Take the prefab that you want to Instantiate and put it in a folder (you will have to create it in the project folder) called "Resources". Place the prefabs that you wish to Instantiate in there. In this case the prefab is called "Wolf".

Then change this line:

var NewGameObject=GameObject.Instantiate(WolfObject,Position,Quaternion.identity);

to:

var NewGameObject=GameObject.Instantiate(Resource.Load("Wolf"),Position,Quaternion.identity);

And now it works! :)

Comment
Add comment · Show 4 · 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 robertbu · Mar 13, 2013 at 05:26 PM 0
Share

Was your prefab not already in the 'Assets' folder? Your original code should work if your prefab was anywhere in the Assets folder.

avatar image RoloJo · Mar 13, 2013 at 05:30 PM 0
Share

Ah, ok that must be it. The prefab was in the project folder.

Cheers :)

avatar image MD_Reptile · Mar 13, 2013 at 07:17 PM 0
Share

Erm... The project folder contains all the assets? That's strange - it should have worked before if all you did was move it. Well hey it works now, good luck!

avatar image jasonFHH · Apr 24, 2013 at 09:51 AM 0
Share

I have a problem similar to this one except I cannot instantiate the WWW-delivered asset bundle, but I can instantiate the local prefab (as loaded from the Resources directory) that it is based on. It gives me the same "ArgumentException: The prefab you want to instantiate is null" error. As others have mentioned, I too thought your original code should've worked if it was properly connected in the Inspector.

avatar image
1

Answer by colorpillar · Oct 15, 2015 at 06:13 PM

Hello, if you wish to organise your prefabs in a subfolder of Resources you must first make a folder named "Prefabs" within Resources. Move your subfolder there.

In Javascript you can then load thusly: var NewGameObject : GameObject = Resources.Load("Prefabs/Your SubFolder/Your Prefab"),Vector3.zero,Quaternion.identity);

You must also remove the ".prefab" extension from your FileName string. ,

Comment
Add comment · Show 1 · 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

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

15 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

Related Questions

instantiated prefab doesn't follow spawn rotation 1 Answer

Call function for instantated prefab. "Object reference not set" 1 Answer

Problem with Prefab - Following cursor 0 Answers

Instantiate Random Prefab From Folder 1 Answer

How to Change the Variables of an Instantiated Object? 0 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