Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Khudere · Apr 08, 2017 at 12:53 AM · gameobjectarray2d gameunique-identifiers

Find GameObject with value within script

Hoping to get some help here, I may be doing things a little backward and still new to Unity

I have GameObjects that are instantiated with pre-assigned unique ID values in an attached script and then have a menu open for each GameObject when requested (double click said object). I can get the values from the GameObject just fine to display but i'm having a hard time understanding how to assign values back to the unique ID GameObject.

I was reading maybe doing a search for all GameObjects into an array and then searching for said unique ID inside their attached script?

I'm working in 2D if that helps at all

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 Khudere · Apr 08, 2017 at 04:31 AM 0
Share

I found a different solution to this, but am still interested if anyone has the answer.

Because when i instantiate the GameObject i created a way to make each name unique and then later i can do a GameObject.Find(UniqueID) and then modify the values that way.

avatar image Pengocat · Apr 08, 2017 at 09:00 AM 0
Share

Can't you just use Object.GetInstanceID ? Why do you want to modify an ID?

2 Replies

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

Answer by IggyZuk · Apr 08, 2017 at 02:35 PM

You can use a Dictionary of GameObjects with id as the key.

 Dictionary<int, GameObject> objects = new Dictionary<int, GameObject>();
 int nextObjectId = 0;
 
 public void AddObject()
 {
     objects.Add(nextObjectId, new GameObject(nextObjectId.ToString()));
     nextObjectId++;
 }
 
 public GameObject GetObjectWithId(int id) {
     if (objects.ContainsKey(id)) {
         return objects[id];
     }
     Debug.LogWarningFormat("Object with id: {0} doesn't exist!", id);
     return null;
 }
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 Khudere · Apr 08, 2017 at 02:37 PM 0
Share

Thanks! I was thinking i may need this in the future!

avatar image
1

Answer by Zymu · Apr 08, 2017 at 11:03 PM

If you want it to be a little more efficient than a GameObject.Find() you can store all instantiated items in a list or an array and create a search function to find the one you want.

 using System.Collections.Generic;
 
 ...
 
 List<GameObject> objectList = new List<GameObject>();
 
 void InstantiateObject() {
     GameObject go = Instantiate(myObject) as GameObject;
     objectList.Add(go);
 }
 
 GameObject FindObjectByID(string ID) {
     GameObject result = null;
     for (int i = 0; i < objectList.Count; i++) {
         if (objectList[i].GetComponent<(your class)>().id == ID) {
             result = objectList[i];
             break;
         }
     }
     return result;
 }

If you're not working with a ton of objects, a tag or name GameObject search will be fine. This just reduces the amount of items you have to sift through to find the one you're trying to find. If you're working with very few items, you can store instantiated objects to global variables the same way.

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

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

Related Questions

Unload from memory - GameObject in an array? 1 Answer

How to set to game objects's position from 2 different game objects arrays equal to each other? 0 Answers

What is the best way to instatiate an array of connected GameObjects? 0 Answers

Transport unknown amout of objects with GameObject array 0 Answers

View an array of the transform position/rotation of all game objects with a specified 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