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 /
avatar image
0
Question by jpostma1 · Mar 10, 2014 at 09:32 PM · instantiateresources.load

Load multiple instances of a prefab through code

Hello,

I'm trying to load multiple instances of a prefab in my script, it works if I put one instance of the prefab on the scene and assign the variable "playerClone" and then run this code:

     public Circle playerClone;
     public List<Circle> players;
     void Start () {
         players = new List<Circle>();
         for (int i = 0; i < 10; i++)
         {
             players.Add((Circle)GameObject.Instantiate(playerClone, new Vector3(i * 1, i * 1, 0), transform.rotation));
         }
     }


But I don't like to have this dummy instance on my scene I think you can imagine why, after all there is a reason why I wan't to instantiate using code.

I thought I could work around this by using "Resources.Load" so I wrote this code:

     public List<Circle> players;
     void Start () {
         players = new List<Circle>();
         for (int i = 0; i < 10; i++)
         {
             players.Add((Circle)GameObject.Instantiate(Resources.Load("circle"), new Vector3(i * 1, i * 1, 0), transform.rotation));
         }
     }

Now I don't need the dummy instance anymore, but instead of getting 10 circles there is only one on the screen and also only one in the Hierarchy tab (at run-time, otherwise there are none)

Do you know any way to instantiate multiple prefabs without the need of dummy instances on the scene?

Thanks in advance!

Jelle,

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

Answer by supernat · Mar 10, 2014 at 09:41 PM

Jelle, unless I misread your question, you want to drag the prefab out of the Project folder (not out of the Scene Hierarchy) and drop it on your playerClone variable in the editor. Otherwise, it is creating a clone of the instance in the scene rather than of the actual prefab.

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 jpostma1 · Mar 10, 2014 at 09:53 PM 0
Share

Nice xD

Thanks a lot,

I still don't get why the Resouces.Load approach results in only one instance tho.

avatar image
0

Answer by shawn-wang · Mar 10, 2014 at 10:08 PM

the list item you want to store is type Circle, but the return of GameObject.Instantiate is type GameObject, you have to use GetComponet method of GameObject to get the right type

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

22 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

Related Questions

Problem with missing texture with Instantiate( Resources.load ) method 0 Answers

which is better : putting all files(Images, AudioClips, ...) in Resources and make one generic prefab ? OR make many Prefabs and instantiate them in Runtime ? and why?! 0 Answers

Checking if object intersects? 1 Answer

instantiate spawning meany objects in multiplayer 0 Answers

Instantiate prefab (Resources.Load) 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