Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


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