Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by ina · Dec 23, 2010 at 02:53 AM · gameobjectinstantiateobjectruntimereference

Referencing instantiated objects at runtime

How do you reference objects that are created at runtime?

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 dissidently · Dec 23, 2010 at 04:26 AM 0
Share

I think what you mean is this?... you instantiate a bunch of objects at runtime, and then later want to be able to reference one or all of them, either to do something to them, or check what they're upto. Is that right?

avatar image ina · Dec 23, 2010 at 04:28 AM 0
Share

yes basically - but they won't all be instantiated at once. depends on player input

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Dec 23, 2010 at 03:32 AM

var clone = Instantiate (prefab);
// do stuff with the "clone" variable
Comment
Add comment · Show 8 · 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 ina · Dec 23, 2010 at 04:09 AM 0
Share

what if you have N clone's, where N is arbitrary?

avatar image Eric5h5 · Dec 23, 2010 at 04:24 AM 0
Share

You can use an array.

avatar image ina · Dec 23, 2010 at 04:29 AM 0
Share

wouldn't you have to know the size of the array before declaring it? N times is unknown, and it depends on how many times the player clicks during runtime - and it's not all at once

avatar image Eric5h5 · Dec 23, 2010 at 04:39 AM 0
Share

No, just use a list. var clones = new List.<GameObject>(); clones.Add (Instantiate (prefab));

avatar image ina · Dec 23, 2010 at 06:50 AM 0
Share

okay - if these prefab's are moved after being declared, assigned to the clones variable, for example, would their transforms still be updated? - also, is it better to use tags or to just keep the variables in memory?

Show more comments
avatar image
0

Answer by Justin Warner · Dec 23, 2010 at 03:30 AM

Could give it a tag, if their are multiple ones (Bullets or enemies?), you could make an array of FindObjectsWithTag or whatever...

If just one though, you can always just use tags.

Comment
Add comment · Show 2 · 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 ina · Dec 23, 2010 at 04:10 AM 0
Share

how do you assign tag's at runtime?

avatar image Justin Warner · Dec 23, 2010 at 08:41 PM 0
Share

$$anonymous$$ake it a prefab with the tag, and instantiate the prefab.

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

No one has followed this question yet.

Related Questions

How can I replace an object while keeping the references to that object? 2 Answers

Reference by Instance ID? 2 Answers

What should I change in this instantiating a GameObject in a script using Unity? 1 Answer

Creating a reference gameobject of a prefab 0 Answers

The problem is into first script, Update function. I cant achieve _player.CurrentHealth and cant understand why. Help pls 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