Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Samur · Dec 09, 2009 at 01:21 PM · gameobjecteditorinstantiate

Finding multiple objects by the same tag as to place them in one array?

I want to make a card game. Every card object will behave the same, so I created a prefab. I created a main object that listens for the cards values and adds them to one array. Index of this array is the number of the card. So if I have 3 cards they will will have numbers: 0, 1 and 2. And here is my problem, with these indexes. I don't want to change them every time I add a new prefab to the scene from the editor. I thought about to answers to my problem: 1) When the prefab is put onto the scene its index number goes up by 1. But that's probably difficult and in order to work it should tracks index number of the last card put. 2) On Start () I wanted to search for every object the has tag: Card and put them all into the array, and then access them.

Neither of these I don't know how to implement. Or maybe there is simpler solution? Thanks :)

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 Bampf · Dec 09, 2009 at 02:04 PM

It's straightforward to find all objects with a particular tag. See the documentation for GameObject.FindObjectsWithTag. The example (JavaScript) code is probably very close to what you need.

If you are new to Unity's arrays, see this handy overview page. FindObjectsWithTag returns an array, and this may be exactly the array you need. But if you want to filter or sort the cards somehow, you can also see in the sample code how to walk the list of GameObjects you got back.

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 Samur · Dec 10, 2009 at 11:44 AM 0
Share

Thanks. It helped :)

avatar image
2

Answer by burnumd · Dec 09, 2009 at 02:02 PM

I can't say I'm entirely certain I understand what your end goal is, but if you're not instantiating cards at run-time, you can use GameObject.FindGameObjectsWithTag(tag : String) to get an array of GameObjects with the same tag. If you do use this approach, you should only do it in Start() (or some other method that gets called only once). If you want to instantiate cards at run-time and monitor them with some global entity, I suggest you add a method to that monitor script that allows cards to register with it, along the lines of:

var allCards : Array = new Array ();

function RegisterCard (card : Card) : void { allCards.Add(card); }

Be sure to check out the docs on Arrays. And then each Card script would have something like the following:

function Start () : void {
    var cardMonitor : CardMonitor = GameObject.FindObjectOfType(CardMonitor);  //Where "CardMonitor" is whatever the script is that keeps track of cards;
    if (cardMonitor) {
        cardMonitor.RegisterCard(this);
    }
}
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 Samur · Dec 10, 2009 at 11:47 AM 0
Share

Thanks. It's also very helpful. For now I'm Using unity as an editor to prototypye, but afterwards for sure your method will be great!

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

No one has followed this question yet.

Related Questions

Why my prefab is auto changing? 3 Answers

Is there a way to edit an instantiated prefab without modifying the prefab itself? 2 Answers

in-editor cloning of game objects 1 Answer

Access a instantiated gameobject from a enemy spawner 4 Answers

Rotate "ghost object" then instantiate object with that rotation 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