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 Musty · Dec 03, 2013 at 03:13 PM · gameobjectinstantiatelistadding

Cant instantiate from one list to another?

Hey guys im trying to create a list of prefabs that i drag and drop in the editor, and then instantiate them in a new list with this code but it doesn't seem to work, i get heaps of errors. I looked at other answers on the forum and tried them but all of them give me the same error i cant figure out why :S

 {
     public List<GameObject> prefabs = new List<GameObject>();
     public List<GameObject> prefabPool = new List<GameObject>();
     public int numberOfPrefabs = 0;
     
     
     
     
     // Use this for initialization
     void Start () 
     {
         for (int i = 0; i < numberOfPrefabs; i++)
         {
 
             prefabPool.Add(Instantiate(prefabs) as GameObject); 
             
         }
     }


 The errors i get are 
 #1' cannot convert System.Collections.Generic.List<UnityEngine.GameObject>' expression to type `UnityEngine.Object'
 System.Collections.Generic.List<UnityEngine.GameObject>.Add(UnityEngine.GameObject)' has some invalid arguments
 #1' cannot convert `object' expression to type `UnityEngine.GameObject'
 The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object)' has some invalid arguments



Thanks for anyone reading this, i just cant get my head around it :/

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

Answer by CHPedersen · Dec 03, 2013 at 03:26 PM

You're getting this error because you're trying to feed Instantiate a list of prefabs, and there is no overload that takes as input a whole list. See its documentation at:

http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

Notice that the docs say the first argument, the "original" should be of type "Object". Now look at what you're giving it - your variable "prefabs" is of type "`List`". Do you understand the error message now? It says it can't convert the List you gave it to the Object Instantiate expects.

Try this instead:

 // Use this for initialization
 void Start()
 {
     for (int i = 0; i < prefabs.Count; i++)
     {
         prefabPool.Add((GameObject)Instantiate(prefabs[i]));
     }
 }
Comment
Add comment · Show 5 · 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 CHPedersen · Dec 03, 2013 at 03:29 PM 0
Share

PS: I would desperately like to surround the "List" with less-than / greater-than to include "GameObject" as the type parameter, but I simply cannot get Unity Answers' idiotic parsing logic to accept those characters in my text. They get removed whenever I hit send.

avatar image Musty · Dec 03, 2013 at 03:32 PM 0
Share

Awesome that works, thanks for you're help. Sometimes its hard for me to understand the docs :/ but thanks for pointing it out now it makes alot more sense :)

avatar image CHPedersen · Dec 03, 2013 at 03:47 PM 0
Share

No problem, everybody starts somewhere. :)

avatar image Kiloblargh · Dec 03, 2013 at 04:31 PM 1
Share

You just need to put spaces, then the List.() isn't mistaken for a html tag by the forum.

The spaces are also necessary (at least they used to be) if you were trying to declare a List.< List. > , which is a useful thing to have...

avatar image CHPedersen · Dec 04, 2013 at 09:38 AM 0
Share

Thanks for the tip, $$anonymous$$iloblargh!

I shall test immediately:

List();

Aha! It works. :)

avatar image
2

Answer by Roland1234 · Dec 03, 2013 at 03:32 PM

You forgot to index your prefabs list so are instead attempting to pass an entire list to the Instantiate method, which only accepts Object references - like this:

 void Start ()
 {
     for(int i = 0; i < numberOfPrefabs; i++)
     {
         prefabPool.Add((GameObject)Instantiate(prefabs[i]));
     }
 }

Also, keep in mind that List exposes its own .Count property that returns the number of items in the list without causing it to enumerate - so keeping a separate numberOfPrefabs counter is unnecessary (just minor FYI, it opens up the possibility of a separate variable being incorrect at some point).

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 Musty · Dec 03, 2013 at 04:40 PM 0
Share

Awesome thanks :) that should help :)

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

19 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

Related Questions

Instantiate an object and place it on a list. 1 Answer

Destroy and rebuild game objects from lists 1 Answer

cant get characters to load 1 Answer

Instantiate random from a list gives error: "Reference not set to an instance" 1 Answer

How to put gameObjects to the list? 4 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