- Home /
Adding id variable # to instantiate-ed object
Hello
I'm just wondering if there is a way to give instantiated objects a unique id variable
The script I am working with, and thought should work is:
var maxObjects : int = 12; var lastgeneratedObject : int = 0;
function Update () { if (lastgeneratedObject <= maxObjects ) { var generatedObject = Instantiate(prefabName, transform.position, transform.rotation); generatedObject.GetComponent(scriptAttachedToPrefab).variableInScriptAttachedToPrefab = lastgeneratedObject; lastgeneratedObject ++; } }
The reason being I want the different numbered instantiated objects to do different things. At the moment all the objects are receiving variableInScriptAttachedToPrefab equal to the maxObjects var (12), instead of 1, then 2, then 3, up to 12.
Answer by duck · Apr 08, 2010 at 09:09 AM
Your logic is sound - what you have should work.
However from the symptoms, it sounds like you've made your "variableInScriptAttachedToPrefab" a static variable.
Remove "static" from its definition, and it should work as expected.
Answer by qJake · Apr 08, 2010 at 08:01 AM
You could do a few different things.
- Set the name of the game object to be the number, using
gameObject.name = number;
or something similar to that. - Use Tags.
- Create a very small component script that simply holds the value for you. You could literally do it in one line:
public int id;
Just name that "ID.js" or something similar, and then you can get the component "ID" and check its value.
Thanks for your quick reply =) But Ducks reply to simply remove "static" from my var did the trick -_-.
I am using the na$$anonymous$$g, but want the scripts on the objects themselves to do certian tasks depending on thier id (ie - basing the xyz positions on 150 + id*width + id*spacing (I even tried converting the names into id vars lol :P -_-)
Tags - I have been trying to avoid them as much as possible, I figured having to do the find command would be somewhat expensive compaired to just linking to the object/script directly =/
Your answer
Follow this Question
Related Questions
Passing a variable to the child of Instantiated object? 1 Answer
How to make script affect only one this game object 2 Answers
How to store a variable of the player 1 Answer
Want to add ship's velocity to Instantiated projectile 2 Answers
Instantiated object affecting variables on the object that instantiated it 1 Answer