- Home /
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 .......
If you know it's 7 for sure, just keep a reference to the boxes using an array.
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; }
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...
@whydoidoit The link to Unity Serializer is broken.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073224im_/https://answers.unity.com/themes/thub/images/avi.jpg)