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 uberokeer · Jul 03, 2013 at 06:03 PM · listvariablesgameobject.findgameobject.findgameobject

Get list of gameObjects and change booleans within them

Hello, Basically what I want to do is get a list of gameObjects that has the tag: "SetObject" and then i want to check a boolean within them called: "isBuild" and if it's true i want to put it in a array of gameObjects i want to do this with hundreds of objects. Then for each one i want to instantiate a object on top of the objects in the array. I'm guessing it would go something like this:

 var gos : gameObject[];
 gos = gameObject.findobjectsWithTag("SetObject")
 foreach (object in gos){
 if (object.containsVariable(isBuild) = true)
 instantiate(prefab, Vector3(object.X, object.Y, object.Z), Quaternion.identity);
 }

Thanks in advance if you actually know how to do this. :P

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
1

Answer by Oribow · Jul 03, 2013 at 06:38 PM

Your way of thought is generally correct. To store a variable in a GameObject you have to use a script.

Try this one:

 var gos : GameObject[];
     gos = GameObject.FindGameObjectsWithTag("SetObject");
     foreach (g in gos){
     if (g.GetComponent("here the name of the Script").isBuild)
     Instantiate(prefab,g.transform.position, Quaternion.identity) as GameObject;
     }

Please keep in minde: isBuild must be public!

Good Luck :)

Comment
Add comment · Show 6 · 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 uberokeer · Jul 03, 2013 at 07:59 PM 0
Share

What is the: "g" suppose to mean I keep getting errors:

$$anonymous$$ identifier: 'g'.

Thanks though :D

avatar image tigertrussell · Jul 04, 2013 at 01:21 AM 0
Share

Put a "var" in front of it. foreach (var g in gos)

avatar image Oribow · Jul 04, 2013 at 08:40 AM 0
Share

Sorry i write more Code in C# as in js so i forgot the var.

avatar image tigertrussell · Jul 04, 2013 at 08:48 AM 1
Share

Then you should look at (and upvote/accept) my answer, below :D Edit: Lol just realized who wrote what. Whoops.

avatar image uberokeer · Jul 04, 2013 at 04:16 PM 0
Share

Well i have another problem i don't seem to understand. I'm getting the error `BCE0019: 'isBuild' is not a member of 'UnityEngine.Component'. it says it's on the line: "if (g.GetComponent("here the name of the Script").isBuild);"

Show more comments
avatar image
0

Answer by tigertrussell · Jul 03, 2013 at 06:31 PM

You cannot arbitrarily add properties or attributes to the GameObject class.

Instead, you'll need to make a script that contains the information that you're hoping to store, and attach that script to the GameObjects in question.

For instance, and I apologize, as I use C# and not JS:

 GameObject gos[] = GameObject.FindGameObjectsWithTag("SetObject");
     foreach(GameOobject object in gos) {
        //This is my custom component/script
        Storage s = object.GetComponent<Storage>();
        //Make sure we have one and that its set.
        if(s != null && s.isBuild) {
           Instantiate(prefab, object.transform.position, object.transform.rotation);
        }
     }

I hope this answered your question.

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.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

A node in a childnode? 1 Answer

Question about variabl lists 1 Answer

Find a GameObject without a reference 2 Answers

How do I save an array of all different variables? 0 Answers

How to get a List of all Variables in a Class 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