Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Pierre 1 · Jan 24, 2012 at 09:44 AM · instantiategetcomponentid

Instantiate ID

Hello everyone,

I am creating several clones from a prefab. At some time, I want to move these clones, with a specific way.

I'd like to know if it is possible to give something to these clones, such as an ID, to be able to call them later from a script.

For the moment, I am thinking about creating an array, and write something like:

Clones[ID_clone] = instantiate(Prefab)

Or maybe I will explore another solution:

Clone.GetComponent(MyScript).someVariable = ID_clone;

But I don't know how to find the object with a script where variable = ID_clone.

Well, if anyone can give me advice, he is most welcome ^^

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 aldonaletto · Jan 24, 2012 at 10:42 AM

The first alternative is the best when you have the ID and want the object. If you have the object and want its ID, the second alternative is better. I once needed both, and used a mix of these two approaches: defined consecutive IDs to the instantiated objects in Awake, created the array and stored the object transforms in the [ID] element in Start - like this (object script):

static var curID: int = 0; // global variable with the currently available ID static var allObjects: Transform[]; // global array of objects var myID: int; // member variable with this object's ID

function Awake(){ myID = curID++; // assign IDs when creating the objects }

function Start(){ if (allObjects.length==0){ // the first object to execute Start allocates allObjects = new Transform[curID]; // the actual array with the right size } allObjects[myID] = transform; // each object stores itself in the array } This code is suitable for objects created at game start. If you want to create objects during the game, an Array or List should be used.

Comment
Add comment · Show 4 · 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 Pierre 1 · Jan 24, 2012 at 11:34 AM 0
Share

Thanks, I think that's what I need. I'll create my objects during the game, but I understand your logic.

Small question though, what command are you using to find the ID when you have the object ? And are you exploring all instantiated objects to find the one you want ?

avatar image syclamoth · Jan 24, 2012 at 11:39 AM 0
Share

I'm not sure I like the idea of using a static variable for the IDs. I'd much rather have the object in charge of instantiating them assign the IDs.

avatar image aldonaletto · Jan 24, 2012 at 12:29 PM 0
Share

@Pierre: each object has a myID variable assigned at Awake. If I click the object, for instance, I can get its script and read myID to know its ID number.
@syclamoth: I used this to catalog some scene objects at Start - they were created in the Editor, thus there was no creator object (it was me...). If you instantiate the objects during the game, it's much better to have a creator object and let it manage things, like you suggested.

avatar image Pierre 1 · Jan 27, 2012 at 09:40 AM 0
Share

Well finally I ended with a simple solution in which I am changing the name of my instantiated object to "objectname"+id.

avatar image
2

Answer by syclamoth · Jan 24, 2012 at 09:53 AM

Well, if you are trying to reference a specific script on the clones, it'd be easiest to use an array of script instances. If you create all the clones at the same time, you can use

var cloneScripts : MyScript[] = MyScript[numberOfClones];

for(var i : int = 0; i < cloneScripts.Length; ++i) { cloneScripts[i] = Instantiate(Prefab).GetComponent.<MyScript>(); }

Then, you can just reference them by index.

You can also give the clones actual ID numbers in this way- just use the line

cloneScripts[i].id = i;

after you instantiate them (assuming, of course, that the MyScript class includes an integer variable called 'id').

If you are not creating the clones at the same time, you can use this-

var cloneScripts : System.Collections.Generic.List.<MyScript> = new System.Collections.Generic.List.<MyScript>();

Then, whenever you spawn a clone use

cloneScrips.Add(Instantiate(Prefab).GetComponent.<MyScript>());

Now, you have a list of clones that you can access with an integer index. You should keep a count in your controller script of the 'next available id' so that you know what you are up to. Otherwise, you can use cloneScripts.Count to find out how many you have at any given time.

Comment
Add comment · Show 4 · 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 Pierre 1 · Jan 24, 2012 at 10:32 AM 0
Share

I am not creating the clones at the same time. Could you be a bit more explicit on your solution ? This is a bit unclear for me.

Great thanks.

avatar image syclamoth · Jan 24, 2012 at 10:41 AM 0
Share

Well, I'm afraid I don't quite understand the specifics of what you will be using the 'id numbers' for. The details really depend on exactly what they're supposed to be able to do.

avatar image Pierre 1 · Jan 24, 2012 at 10:54 AM 0
Share

Well, this is a server / client boardgame game with a fully authoritative server. On the server, I create a token which represents a player. When, on the client, the player gives a move order, I want to transfer the order to the token on the server. Therefore, I'd like to give to the token the player id, so that when I receive a move order from a player, I can identify the token to move.

I hope I am clear ^^

avatar image syclamoth · Jan 24, 2012 at 11:05 AM 0
Share

I see. Are you using inbuilt networking? The NetworkView kind of provides all of this functionality.

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

6 People are following this question.

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

Related Questions

Assigning id to instantiated objects 1 Answer

Access a function of an instantiated prefab 1 Answer

How do I acces a specific GameObject within the terrain???? 1 Answer

Spawn a prefab on another object based on ID? 2 Answers

instantiate prefab and link 3rd party gameobject script 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