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 Mariol · Apr 11, 2012 at 10:12 PM · instantiatecomponentsvaluesassign

instantiating multiple objects and assigning them individual values

I'm trying to instantiate 16 objects (in a grid 4x4) and assign each object a value and ID dependent on the random number that is generated when the object is instantiated.

The code below is present on each prefab. The timer on the same object calls the function below and it generates the number to assign a texture to that object, id and value.

 var cube : GameObject;
 
 function SwitchBlock(){
     setupBoard.randomNumber = Random.Range(0,setupGame.textureArray.length);  
     cube = Instantiate(cube, transform.position, transform.rotation);
        cube.renderer.material.mainTexture = 
         Resources.Load("Textures/" + setupGame.textureArray[setupBoard.randomNumber], Texture2D); 
     this.GetComponent(setLetter).points = setupGame.setLetters[setupBoard.randomNumber,1];
     this.GetComponent(setLetter).id = setupGame.setLetters[setupBoard.randomNumber,0];
     print(cube.GetComponent(setLetter).points);
 }

The problem that I am having is that when I click on any block that's instantiated, I get the value and ID of the last block only. I have printed out when all values are assigned so I know that the blocks are getting different values set, but they seem to be getting over written some where.

I think it is because I am instantiating the objects all called the same name, but I'm a beginner and not 100% certain.

Can anyone tell me my mistake? And is there a more efficient way to do this?

Thanks

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
0

Answer by Kleptomaniac · Apr 11, 2012 at 11:15 PM

Are you running this as a calling this method from either your setupGame or setupBoard scripts? Because you don't appear to be making this a coroutine of Update. Also, I believe you should be instantiating as an instance of the prefab ... so as in a new variable. This may be why you're getting the same values. Anyway, this is how I would do it:

 var cube : GameObject;
 
 function SwitchBlock(){
     
     setupBoard.randomNumber = Random.Range(0, setupGame.textureArray.length);  
     
     var cubeInst : GameObject = Instantiate(cube, transform.position, transform.rotation) as GameObject;
     cube.renderer.material.mainTexture = Resources.Load("Textures/" + setupGame.textureArray[setupBoard.randomNumber], Texture2D); 
     cubeInst.GetComponent(setLetter).points = setupGame.setLetters[setupBoard.randomNumber, 1];
     cubeInst.GetComponent(setLetter).id = setupGame.setLetters[setupBoard.randomNumber,0];
     
     print(cubeInst.GetComponent(setLetter).points);
     
 }

I believe that should work.

Hope that helps, Klep

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 Mariol · Apr 12, 2012 at 08:21 PM

Hey, thanks for the help but unforutnately it hasn't worked :(

I'm calling the above from this (which is placed above the code I asked about before):

 var switchTime = 3;
 var seconds = switchTime;
 
 function Start () {
     InvokeRepeating ("Countdown", 1.0, 1.0);
 }
 
 function Countdown () {
     seconds -= 1;
     if (seconds == 0){
     
         if (gameTimer.timer == 0){
             CancelInvoke("Countdown");
             gameTimer.timer = 30;
             Application.LoadLevel ("mainMenu");
            }
         else {
             seconds = switchTime;
             Destroy(gameObject);
             SwitchBlock();
         }
     }
 }

I'm not calling this within an update function as the start function calls the countdown timer which in turn calls the switchBlock function.

I'm kind of stuck now. Any other advice?

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 Eugenius · May 11, 2013 at 08:16 AM 0
Share

Try doing what $$anonymous$$leptomaniac specified - var cubeInst : GameObject = Instantiate(cube, transform.position, transform.rotation) as GameObject; - ins$$anonymous$$d of instantiating 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

6 People are following this question.

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

Related Questions

Can't assign dictionary values 0 Answers

Issue instantiating an object with button click 2 Answers

Assigning instantiated objects to a List of a Toggle button 2 Answers

Will Instantiating prefab with many components slow my game? 1 Answer

is it possible to link 2 separate component fields in the same game object 0 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