I want to place gameObjects (like buildings etc) selected from an array during runtime. There will be many gameObjects, so I want to avoid manually assigning them in the inspector.
Is there a smooth way to sort/set-up gameObjects so they can be entered into an array automatically?
Any help appreciated, cheers!
asked Apr 14 '10 at 03:01 PM
You must have some kind of criteria for selecting them. Typically you'd want to collect them by name, or by tag, or by whether they have a specific component (or script) attached. There are various ways to do this:
By Component (or script), for example to collect together every item in the scene which has the "PowerUp" script attached, use FindObjectsOfType. Eg:
By Tag, for example to collect all items which have been assigned the "PowerUp" tag, use FindGameObjectsWithTag, like this:
And by name, (which is the slowest method - although still sometimes useful), for example to collect all items which include the word "PowerUp" in their name, you have to first find all gameobjects, and then scan through their names. Eg:
answered Apr 14 '10 at 04:24 PM
That should solve your problem. the error
Tells you exactly what is wrong.