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
1
Question by NerdRageStudios · Mar 06, 2015 at 09:37 PM · arrayfindgameobjectswithtag

How to find a certain Gameobject using a variable, in a common script from an array of gameobjects?

Hi all, I have a need to locate a certain gameobject in the scene at runtime.

I have a whole bunch of doors that are all from instantiated prefabs, but when they are created they are given a unique door number. Basically, I need to find that number in script and assign it to a target.

Here are excerpts of my scripts:

 GameObject[] doors;
 doors = GameObject.FindGameObjectsWithTag("Door");

Ok, so now I have an array of Doors, that all have the script "Door Controller" on them. There is a unique int called "Door Number".

So, my question is how can I find the gameobject in this array, that has a certain door number?

Any help would be greatly appreciated :)

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

Answer by Addyarb · Mar 06, 2015 at 09:47 PM

There are several ways to do it, but the most intuitive way (for me) is to use something called a foreach loop!

Here's what I would do.

 public GameObject doorIAmAccessing; //Declare this outside the function (at the top of script)
     
     int numberIAmLookingFor = 9; //Define the door number we want. This can be done in many ways.
     
     foreach (Gameobject door in doors){ //For each (GameObject) door in the list of doors...
     int doorNumber = door.GetComponent<scriptOnTheDoor>().doorNumber; //doorNumber is the number on the door's script.
     if(doorNumber == numberIAmLookingFor){ //If the door number on said door is the number I'm looking for...
     doorIAmAccessing = door; //This is the door!
     
     }
     } //If it isn't, the foreach loop will automatically run until it gets to the end of the list. 
 

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 NerdRageStudios · Mar 06, 2015 at 10:48 PM 0
Share

Legend! that worked a treat, many thanks :)

avatar image
1

Answer by vikingnm · Mar 06, 2015 at 10:55 PM

if all the doors have the script on them maybe adding a public int variable inside your script called doorNumber or something like that and assign the number for each door in the editor

then a loop through the doors array and a check for that variable should work

     GameObject[] doors;
     doors = GameObject.FindGameObjectsWithTag("Door");
 
     GameObject tempDoor;
 
     foreach(object door in doors) {
         if(door.GetComponent<SCRIPT_NAME>().doorNumber == DOOR_INT {
             tempDoor = door;
             break;
         }
     }
 

however, there's probably a more efficient/better way...i'm still learning Unity myself

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

22 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

store GameObject.FindGameObjectsWithTag in an array 2 Answers

AddComponent To GameObjects On Array 1 Answer

FindGameobjectsWithTag -> Array 2 Answers

Check distances of multiple objects, and do something if all outside of range 2 Answers

finding clones and destroying them doesn't work 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