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
0
Question by Sara1996 · Dec 08, 2015 at 03:53 AM · c#unity 5prefabsbuttonsinstances

How to load instances of a prefab to an array c#

Hello, I'm trying to make instances of a prefab change their positions one at a time when a button is clicked. my idea is to make an array of the prefab instances using find tag then use it in onClick function. My code doesn't work and I don't know how to fix it. Please help

 public GameObject[] balloons;
 // ?? I don't know how to add the instances to the array 
 
 public void OnClick()
     {    if (GameManager.instance.WrongButtonSelected == false && currentSelected == false)
         {
             GameManager.instance.WrongButtonSelected = true;
             currentSelected = true;
             for ( int i=0;i<balloons.Length;i++){
                 Debug.Log (gameObject.name);
                 transform.Translate(Vector2.up*speed*(Time.deltaTime));
                 break;
             }    
         }    
     }



Thank you in advance

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 callen · Dec 10, 2015 at 02:55 PM

If you are creating these 'instances of a prefab' using Instantiate() in your code, you would likely be adding them to the array at the same time, like

 for(int i=0;i<balloons.Length; i++)
   balloons[i] = Instantiate(BalloonPrefab.gameObject) as GameObject;    

On the other hand, if these are instances you're creating in the editor, you can drag-and-drop them into the array. Just take each of the GameObject instances from the Hierarchy panel, and drag into the public, editor-visible Balloons array, on whatever object has this script.

Now, onto the matter of your logic, I also dont think that will give the results you desire. Your for-loop, since it uses a break statement, will only ever move the first (index-0) element. I think if you tried something like this, it would give you what you want "change their positions one at a time when a button is clicked"

 int index = 0;
 
 public void OnClick() {    
         if (GameManager.instance.WrongButtonSelected == false && currentSelected == false)
          {
              GameManager.instance.WrongButtonSelected = true;
              currentSelected = true;
 
              Debug.Log (balloons[index].gameObject.name);
              Vector2 moveDelta = Vector2.up*speed*(Time.deltaTime);
              balloons[index++].transform.Translate(moveDelta);
 
              index = index % balloons.Length;
          }    
      }
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 Sara1996 · Dec 14, 2015 at 12:58 PM 0
Share

Thanks, it works perfectly! :D

avatar image
0

Answer by JedBeryll · Dec 10, 2015 at 02:24 PM

This should work if you set the gameobject tags correctly.

 balloons = GameObject.FindGameObjectsWithTag("myTag");
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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a crouch button for mobile? 2 Answers

How to count collisions of the same tagged objects? 2 Answers

Game Object is Instantiating on same position 2 Answers

How Can I load Different Prefab on Combobox Change? 0 Answers

I want to open loot chest prefabs individually in the scene 3 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