• 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
1
Question by tperry1x · Oct 27, 2015 at 09:29 AM · instantiate prefab

Instantiate a prefab which exists in assets, not in scene

Hope someone can help with this. I have a prefab called 'brick' (it's just a red cube). This prefab is in a folder called 'prefabs' inside my assets folder. It's not in the scene.

In javascript, I'm trying to instantiate the prefab using:

 var brick : Transform;
 function Start () {
     var y = 0; 
     var x = 0; 
             Instantiate(brick, Vector3 (x, y, 0), Quaternion.identity);
         }
     }
 }

But I get the error:

 ArgumentException: The prefab you want to instantiate is null.

What does this mean? – I wish Unity's errors were more n00b-friendly!

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
Best Answer

Answer by ARKMs · Oct 27, 2015 at 03:42 PM

I recommend you read about Resources: Resources

But it would be like

 var instance : GameObject = Instantiate(Resources.Load("brick", GameObject));


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
avatar image TheGreatCabbage · Mar 03, 2017 at 09:57 AM 0
Share

Great, thanks.

avatar image
0

Answer by tperry1x · Oct 27, 2015 at 09:06 PM

Brilliant, thank you ARKMs. Works like a charm. I'm using

 var instance : GameObject = Instantiate(Resources.Load(myLitem, GameObject));

Where myLitem is the GameObject I used using the GameObject.find earlier up in the script. One question: Although this works well, the instantiated object always appears at the x y z co-ordinates of 0. One thing I couldn't work out was how would you get the instantiated object to appear at the location of a GameObject? @ARKMs

Comment
Add comment · Show 2 · 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 tperry1x · Oct 27, 2015 at 09:46 PM 0
Share

Actually, just solved it. I used: var instance : GameObject = Instantiate(Resources.Load(myLitem, GameObject),transform.position + Vector3(0,0.2,0),transform.rotation);

avatar image ARKMs · Oct 28, 2015 at 12:37 AM 1
Share

Yeah, in you var 'instance' you can instance .transform.position = otherGameObject.position; intance.transform.rotation = otherGameObject.rotation;

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

33 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

Related Questions

Instantiating object at a specific location? 1 Answer

Cannot instantiate 2D prefab on mouse click 1 Answer

How to Instantiate a prefab with radius using OverlapCircleAll ? 0 Answers

Prefabs not instantiating after build 0 Answers

Why do my first 50 instances of a prefab have the same properties? 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