Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by ZanzibarDreams · Apr 14, 2010 at 03:01 PM · arrayruntimegameobjectsfill

Filling array with gameObjects

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!

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 duck · Apr 14, 2010 at 04:24 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:

var powerUps = FindObjectsOfType(PowerUp);

By Tag, for example to collect all items which have been assigned the "PowerUp" tag, use FindGameObjectsWithTag, like this:

var powerUps = FindGameObjectsWithTag("PowerUp");

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:

var objects = FindObjectsOfType(GameObject);
var powerUps = new Array();
for (var  obj : GameObject in objects) {
    if (obj.name.Contains("PowerUp")) {
        powerUps.Add(obj);
    }
}
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 ArcIo · May 14, 2012 at 08:26 PM 0
Share

No matter how I try this, I get "UnityException: You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration."

avatar image
0
Wiki

Answer by KingCharizard · May 15, 2012 at 03:05 AM

 var levelTriggers;

 function Start() {
  levelTriggers = FindObjectsOfType(ColTrig);
        if (levelTriggers.length > 0){
        print (levelTriggers);
     }

}

That should solve your problem. the error

125*UnityException: You are not allowed to call this function when declaring a variable.*125

Tells you exactly what is wrong.

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 ArcIo · May 15, 2012 at 03:27 AM 0
Share

First off, sorry for the double post. Secondly, I'm trying to understand this. Are you saying that because you're defining the variable levelTriggers outside of the Function, it's global and I can't declare an array of things a global variable? Or do I have that completely wrong? From what I understand, JS assumes everything is global so putting it inside of a function can make it private, again, is this correct?

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

2 People are following this question.

avatar image avatar image

Related Questions

How to deactivate gameObjects instead of destroying them ? 1 Answer

comparing variables of gameobjects in an array 2 Answers

snapping objects at runtime 2 Answers

Creating a 2d array of gameObjects 2 Answers

Create an array of Vector3 from Editor script 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