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 /
  • Help Room /
avatar image
0
Question by ygliam · Mar 05, 2016 at 04:03 PM · prefabprefab-instance

Instantiate an array of prefabs, and call a function in each prefab.

So I have an array set up for my prefabs and instantiate them, then after I spawn each prefab I want to call a function with a parameter. However all the prefabs are set to the last function called.

 public GameObject Number;
 int num;
 const int width = 7;
 const int height = 5;

 GameObject[,] newNumber;

 void Start ()
 {
     GameObject[,] newNumber = new GameObject[width, height];
     var rand = new System.Random();
     for (int j = 0; j < height; j++)
     {
         for (int i = 0; i < width; i++)
         {
             Vector2 pos = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width * (0.2f + 0.1f * i), Screen.height * (1 + 0.15f * j)));
             newNumber[i,j] = (GameObject)Instantiate(Number, pos, Quaternion.identity);
             num = rand.Next(1, 6);
             Debug.Log(num);
             newNumber[i,j].GetComponent<Number>().initialize(num);
         }
     }      
 }

Code in prefab:

 public void initialize(int no)
     {
         Debug.Log("initialized");
         currentRenderer = this.GetComponent<SpriteRenderer>();
         if (no == 1)
         {
             currentRenderer.sprite = number1;
             number = 1;
         }
         else if (no == 2)
         {
             currentRenderer.sprite = number2;
             number = 2;
         }
         else if (no == 3)
         {
             currentRenderer.sprite = number4;
             number = 4;
         }
         else if (no == 4)
         {
             currentRenderer.sprite = number6;
             number = 6;
         }
         else if (no == 5)
         {
             currentRenderer.sprite = number8;
             number = 8;
         }
     }

Comment
Add comment · Show 6
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 Glurth · Mar 05, 2016 at 06:31 PM 0
Share

it looks like you are already calling a function on your component("Number") on this line. newNumber[i,j].GetComponent().initialize(num);

perhaps I misunderstand. I also don't really understand what you mean by "all prefab are set to last function called".

avatar image ygliam · Mar 06, 2016 at 09:10 AM 0
Share

because I want to send or initialize all of my prefabs to a random number. however say the last prefab created is send a 5, all of my prefabs are set to 5. even though the logged random numbers are all different So whenever I initialize a prefab in the array with a random number, it sets all of the prefabs to that number. Is there any way to individually set each prefab in the array to a separate number.

avatar image Glurth ygliam · Mar 06, 2016 at 10:06 AM 0
Share

it looks like you are setting the number in your initialize function, but we don't have the code for that. Can you edit the question, and add it?

avatar image ygliam Glurth · Mar 06, 2016 at 10:39 AM 0
Share

added prefab script

Show more comments
avatar image ygliam · Mar 06, 2016 at 08:34 PM 0
Share

ah yes, that was it, thanks alot mate, didn't think about that

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Nested prefabs : how to apply position changes on one sub prefab to all instances ? 0 Answers

Changing Values in Inspector Only Takes Effect After Playing the Scene Twice? 0 Answers

Dragging prefab onto object through code 1 Answer

how to change a texture on instantiated prefab? 0 Answers

Call method on prefab without instantiating 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