• 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 Polatrite · Dec 19, 2009 at 06:09 AM · gameobject

How do I iterate through and identify all scene objects?

http://answers.unity3d.com/questions/93/how-do-i-iterate-over-all-scene-objects-from-an-editor-script I used the above technique to iterate through my scene objects. However I'm still unsure on how to identify what these scene objects are - for example I have a "gate" prefab. I want to allow a certain keystroke to open all gates, so I loop through all scene objects, identify that they are of the "gate" type, and then I execute the necessary code to open them.

What is the best way to do the iterating and identifying process? This seems like something that it's practically impossible to develop games without, but my searches didn't turn up hardly anything!

Does Unity handle this type of thing differently?

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

Answer by TowerOfBricks · Dec 19, 2009 at 09:03 AM

You probably have a gate script on each of your gates, then you can search for the Gate script instead of searching for all gameObjects.

var MyGates : Gate[] = FindObjectsOfType (Gate);
for (i=0;i<MyGates.lenght;i++) {
    MyGates[i].OpenGate ();
}

This will probably work. If you don't have a script attached to the gates you can do a search for all objects with a certain tag, a "gate" tag which you mark all your gates with.

C# example:

Gate[] myGates  = FindObjectsOfType (typeof(Gate));
for (int i=0;i<myGates.Length;i++) {
    myGates[i].OpenGate ();
}
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 Polatrite · Dec 20, 2009 at 05:26 AM 0
Share

Is it possible you could post a C# version of this? I'm having a hard time converting this to C#, as I'm a complete newbie.

avatar image TowerOfBricks · Dec 21, 2009 at 12:22 PM 0
Share

Added a C# example.

avatar image DanW · Feb 29, 2012 at 12:10 AM 0
Share

And the first line of the C# example needs to be:

Gate[] myGates = FindObjectsOfType (typeof(Gate)) as Gate[];

See http://unity3d.com/support/documentation/ScriptReference/Object.FindObjectsOfType.html

But the answer helped me, so thanks.

avatar image
0

Answer by Ashkan_gc · Dec 19, 2009 at 04:58 PM

The Stone's answer is correct. also you can use their name to find them. GameObject.Find (string name) is the function that you can use. this function will return null if find no object. the best approach is the code shown above if you have a special script attached to gates. in most cases you have, i think. in this way you can create reusable codes so don't put gate's special code in a general script for gates and doors and windows and ... create a door component and a gate component and ...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to tilt the gameobject based of Input.Acceleration? 0 Answers

How could it be done "Cooldown" for a shield ? 2 Answers

Where to attach a script? 1 Answer

Shooting Specific GameObject via Tag 0 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