• 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 kor · Apr 08, 2010 at 06:13 AM · instantiatevariableidunique

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.

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
1
Best Answer

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.

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 kor · Apr 08, 2010 at 11:50 AM 0
Share

You sir are a god, gentleman and a scholar

avatar image
0

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.

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 kor · Apr 08, 2010 at 11:58 AM 0
Share

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Trouble setting a variable to a game object using instantiate. 1 Answer

Instantiate JS error that i can't figure out 1 Answer

Instantiate new object from scratch 1 Answer

Checking if object intersects? 1 Answer

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


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