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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by el-santia93 · Sep 09, 2013 at 09:13 AM ·

how do i use GameObject.FindGameObjectsWithTag() on lists

 var spawnPoints:GameObject[];
 function Start () {
 spawnPoints=GameObject.FindGameObjectsWithTag("spawn");

}

this works fine but when i change spawnPoints to a list i get this error"Cannot Convert UnityEngine.GameObject[]to Systems.Generic.List..

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

1 Reply

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

Answer by syclamoth · Sep 09, 2013 at 09:20 AM

Remember that while they can often be used in the same way, an array and a List are fundamentally different things. You can't just assign an array to a list reference and expect them to work.

However, the List API comes with a function for doing exactly what you want to do here!

Just use

 spawnPoints.AddRange(/*whatever you need to add*/);

and you are good to go.

Remeber that spawnPoints needs to have been created before you can add stuff to it- you will need to allocate a new List object or you will get a Null Reference Exception when you try to add new items to it.

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 ArkaneX · Sep 09, 2013 at 09:28 AM 3
Share

You can also pass an array returned from FindGameObjectsWithTag to the List constructor:

 spawnPoints = new List.<GameObject>(GameObject.FindGameObjectsWithTag("spawn"));
avatar image shram ArkaneX · Mar 13, 2017 at 02:30 AM 0
Share

Epic! Thanks!

avatar image cjhoffmn ArkaneX · Sep 28, 2017 at 05:53 PM 0
Share

ArkaneX - this was spot on for something I was working on ! Thanks for this. One note - I think you have an extra "." in the code above though. $$anonymous$$aybe it was changed, but I think you don't need the dot in this part between "List" and "<"

Holy crap I can't get the triangular brackets to print! lol...

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

Logical Error In Spawn Counter (Javascript) 2 Answers

2D Spawn with fixed random locations 0 Answers

How to Spawn a Gameobject According to a Timer? 2 Answers

Spawning Creatures 0 Answers

Instantiate stop when all of them have destroyed 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