• 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 firasdeep · Jun 24, 2012 at 07:31 PM · gameobjectstaticid

GetInstanceID ??????

Hello I have 7 GameObjects all of them has same name "Box" and same tag, when I start play every GameObjects has ID but when I reload the level or load other level and back to the box level the ID of GameObjects has been changed.

how to make the ID of GameObjects static ???

thx .......

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 Mizuho · Jun 24, 2012 at 07:44 PM 1
Share

If you know it's 7 for sure, just keep a reference to the boxes using an array.

avatar image firasdeep · Jun 24, 2012 at 08:44 PM 0
Share

no it is random number 7 or more or less

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Jun 24, 2012 at 09:24 PM

That's a big problem, and the solution depends on what exactly you wanna do.

  • If you create the objects during the game, you can use indexed names like "Box1", "Box2" etc (example in the box creator script):

var num = 0;
var boxPrefab: GameObject;
  ...
  // when creating a new box...
  var box = Instantiate(boxPrefab, pos, rot);
  num++; // increment the counter...
  box.name = "Box"+num.ToString(); // and append it to the name
  ...
- If the boxes are created in the editor, set the names manually, or use a static int to numerate them sequentially at runtime (box script):

static var num: int = 0;

function Awake(){ num++; name = "Box"+num.ToString(); } the static variable is the same for all boxes, thus each one will get an unique sequential number. If you don't want to change the name, store the box number in a ID variable:

static var num: int = 0; public var myID: int;

function Awake(){ myID = ++num; }

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 whydoidoit · Jun 24, 2012 at 09:31 PM

I have a solution for this - but it isn't easy. InstanceID will not be saved across the saving and loading of scenes.

Basically I use a static manager component to create a reference GUID for every game object which it then reloads when the scene reloads. Using this technique it is possible to maintain an Unique ID next to every scene object that is tagged with a script.

@Aldonaletto's answer is the way to go if you can make it work. If you can't you can use my Unity Serializer and attach a UniqueIdentifier component to each object you want to a static ID for (plus you get a bonus free load/save solution!) I can try and work through that code with you too if you want to see how it works...

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 Zac - Midnight Studios · May 29, 2015 at 06:41 PM 0
Share

@whydoidoit The link to Unity Serializer is broken.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using Scripts in AssetBundles 0 Answers

Moveing my camera 1 Answer

Refering to gameobject script is attached to 1 Answer

Where to attach a script? 1 Answer

Shooting Specific GameObject via 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