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 Alkan · Oct 13, 2011 at 04:44 PM · instances

Keeping track of multiple instances of one prefab

I'm instantiating a small number (4 to 5 instances) of the same prefab into a single scene, the prefabs are rigidbodies and are spawned in sequence over time (with a for loop) and all appear in the inspector as "nameofprefab(Clone)"

As each instance is used for a scoring mechanism I need to be able to uniquely identifiy each one for collision purposes but am unsure of how to give each instance a unique ID.

What is the best practice for this? Is there a way I can give each instance a unique name when it's spawned? I've heard of "tags" but not had the chance to read about them yet - although I'm not sure if they are the correct way of solving my problem.

Secondary to this, I also need to instantiate a blob shadow projector (for each instance) which will be told to follow it - currently I attach a script to a blob shadow projector which tells it what gameobject to follow in a pre-determined fashion, I'm a bit unsure of how to automatically instantiate multiple instances of shadow projectors which dynamically are able to follow different instances (hopefully with some kind of unique ID) of the same prefab.

I really hope this makes sense!

Can anyone help with these issues?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by BerggreenDK · Oct 13, 2011 at 05:08 PM

Show us your spawn code and we can easily add it I think.

But to explain it in short.

The line where you use the Instatiate function, keep the result in a GameObject variable. Like:

GameObject goTemp = Instantiate(something)...

Then on the goTemp object, you now how ... tradaaa! .name attribute.

I usually do it like this:

 for(int cnt=0; cnt<10 ; cnt++)
 {
    GameObject go = Instatiate(something);
    go.name = "Something_" + cnt; 
 }

To add the number in the end of the name + I am still able to search for "something_" with the GameObject.Find("something_") function

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 Alkan · Oct 13, 2011 at 09:18 PM 0
Share

Hi BerggreenD$$anonymous$$ - your solution was perfect, that's exactly what I was after - I've given it a quick go and it works perfectly - this is my new code (if statement) with your stuff added:

 var i : int = 0;

 if (Input.GetButtonDown("Shoot")&&i<currentlevel.length&&shotdelay==1&&controller.gameplay==true){

 shootdelay();

 var instance : GameObject = Instantiate(currentlevel[i], transform.position, transform.rotation);

 instance.name = "Shape_" + i; 

 i++;     

 }

So my next question would be, how would I instantiate shadow blob projectors for each of these objects?

$$anonymous$$y current shadow projectors have the following script attached:

 var objectToFollow : Transform;

 var offset : Vector3;

 function Update () {

 transform.position = objectToFollow.position-offset;

 }

...which allows me to tell each one to follow a specific game object, but ideally I need to automatically instantiate them in a way where they know which "instance.name = "Shape_" + i;" gameobject to follow

avatar image
0

Answer by chemicalvamp · Oct 13, 2011 at 05:05 PM

If the object has a script attached to it the script can access by transform in example:

 void OnCollisionEnter()
 {
    Destroy(transform.gameObject);
 }

would destroy the object when it is collided with.

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 poncho · Oct 13, 2011 at 05:11 PM

the way i track my objects is to store the reference of that object in a list of gameobjects

List myGameObjectsToTrack;

myGameObjectsToTrack.Add(thelocalGameObjectInstantiated);

myGameObjects[indexOfMyGameObjectIWouldUse].

using events and the gameobjects themselvels, would be a way to solve your issue

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy(this.Object) destroys ALL instances 2 Answers

Do I need to rework whole item system? 1 Answer

GameObject.Find() not working when connected to prefab 2 Answers

Comparing PhysicMaterials at Runtime 3 Answers

How to have unique instances of script on multiple instances of prefab? 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