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 michael 4 · May 08, 2011 at 11:11 PM · gameobjectmaterialschildrenglobal

Return All gameobjects in scene to an array

How might I return all the gameobjects, and their children into an array?

This is so I might loop through and apply a global materials change.

Tagging gameobjects is out of the question unless there is a way to apply a tag to all the children in the gameobject (So i can reference them individually)

Comment
Add comment · Show 2
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 Jessy · May 08, 2011 at 11:54 PM 0
Share

Don't you, ins$$anonymous$$d, want FindObjectOfType(Renderer)? Not all of the Game Objects are going to have materials associated with them. Also, that might not even be necessary. What about Shader.SetGlobal...?

avatar image michael 4 · May 09, 2011 at 12:27 AM 0
Share

how might Shader.SetGlobal... work? It is indeed a global shader change that Im looking to do, but according to the documentation, the only think that might be useful is SetGlobalTexture, but its not the texture I want to change, its the actual shader.

Also I wasnt aware of the FindObjectOfType() function, that might be the answer if the SetGlobal comes up short.

4 Replies

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

Answer by Peter G · May 08, 2011 at 11:52 PM

Have you tried:

var gameObjs : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
Comment
Add comment · Show 2 · 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 michael 4 · May 09, 2011 at 12:28 AM 0
Share

Thanks, like I mentioned above, I wasnt aware of the FindObjectsofType() function thats probably what I was looking for.

avatar image Tomleung · Feb 25, 2014 at 02:11 PM 0
Share

How about if only some objects in the scene but not all gameobjects? Thanks for answering.

avatar image
1

Answer by colinday · Dec 02, 2016 at 06:07 PM

 Scene scene = SceneManager.GetActiveScene();
 GameObject[] rootObjects = scene.GetRootGameObjects();
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
avatar image
0

Answer by beachpong · Mar 14, 2015 at 01:39 AM

Hi,

Let's say you want to access only the gameobjects that you have in your scrpit (e.g script name is DemoScript). To access only these game objects, try this:

object[] obj = GameObject.FindObjectsofType(typeof(DemoScript));

foreach(object o in obj)

{ //do something }

Hope this helps.

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 Tomleung · Mar 14, 2015 at 06:49 AM 0
Share

Hi! beachpong! $$anonymous$$ay I ask you a question? Your "foreach(object o in obj)",if I want to use for-loop ins$$anonymous$$d of using foreach, how should it be? Thx!

avatar image
0

Answer by beachpong · Mar 14, 2015 at 12:11 PM

Hi Tomleung,

Sure. You can try this.

object[] obj = GameObject.FindObjectsofType(typeof(DemoScript));

for(int i=0;i

Comment
Add comment · Show 2 · 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 Tomleung · Mar 20, 2015 at 09:53 AM 0
Share

Why your answer is not completed? Should it be object[] obj = GameObject.FindObjectsofType(typeof(DemoScript));

for(int i=0;i < obj.Count;i++) { obj[i].XXX = XXX } ?

avatar image beachpong · Mar 20, 2015 at 05:39 PM 0
Share

Hi,

Sorry about that. But it doesnt take the anwer I type. You are correct about the loop. But use obj.Length ins$$anonymous$$d of count. print(obj[i]), will for example will print all the gameobjects. Hope this helps :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Best Way - Animation - Multiple GameObjects. 0 Answers

How to change material color to a specific object group's childs..? 1 Answer

Set Specific Child's Material 2 Answers

use list of gameobjects transform in raycast 0 Answers

How to set the transparency of an object? 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