Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 VaderCmp · Apr 15, 2016 at 02:41 PM · c#instantiatebuttonspositioninginstantiate prefab

Instantiate buttons , each with a unique positioning

How could I Instantiate level buttons (like 80 ), each with a unique positioning? I need to set the position manually for each of them. (i know is weird, but this is the desing)

I know that I would need empty gameObjects for each position, but I don't Know how to put this in the Instantiate code. So if anybody has any suggestion, I would really appreciate !

Thank you!!

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 tanoshimi · Apr 15, 2016 at 02:50 PM

Position is the second parameter you pass to Instantiate...

 Instantiate(UniqueButtonPrefab, new Vector3(UniqueButtonX, UniqueButtonY, UniqueButtonZ), Quaternion.identity);

http://docs.unity3d.com/ScriptReference/Object.Instantiate.html

Comment
Add comment · Show 4 · 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 VaderCmp · Apr 15, 2016 at 04:14 PM 0
Share

Thanks for answering! @tanoshimi I was wondering if there is a method to do this for all the button, and not write code for each. I detailed a bit more in the comment above. I would like to see your approach, because I am a self taught C# and It would help me a lot.

avatar image tanoshimi VaderCmp · Apr 15, 2016 at 05:27 PM 0
Share

Of course.

 // populate these via the inspector
 public List<GameObject> Buttons;
 public List<Vector3> Positions;
 
 for (int i=0; i<ListOfButtons.Count; i++){
     Instantiate(Buttons[i], Positions[i], Quaternion.identity);
 }
avatar image Blue-Cut tanoshimi · Apr 15, 2016 at 09:08 PM 0
Share

tanoshimi method works and will be efficient if you have a few buttons to set up.

But you said in your question that you had something like 80 buttons ? Populate your arrays with the inspector is not a good idea. You should define some rules that allow you to "calculate" the properties of the button you are creating.

Show more comments
avatar image
1

Answer by Blue-Cut · Apr 15, 2016 at 03:31 PM

Hello,

What you need to have is a class (a script) that is in charge of instantiating a button. This class knows a prefab that is your button model :

 public class ButtonMaker : MonoBehaviour {
 
     public Prefab buttonModel;
    
     public void CreateButton()
     {
           RectTransform myButton = Instantiate(buttonModel as RectTransform);
     }
 }

Call the CreateButton method for each button you need to create.

Then you put the properties of the button like you want. Complete the ButtonMaker class to be able to set the properties you need as you like.

For example, put the ButtonMaker on the GameObject parent of all your buttons then do :

 myButton.SetParent (transform, false);
Comment
Add comment · Show 3 · 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 VaderCmp · Apr 15, 2016 at 04:06 PM 0
Share

Thanks for answering! @Blue Cut I was wondering if there is a way to make this without writing code for every button.Is is possible to have a List<> where i would store all the positions, and have in Instantiate:

if(spawnPosition.ID = button$$anonymous$$odel.ID) { Instantiate(button$$anonymous$$odel, spawnPosition.position) }

avatar image Blue-Cut VaderCmp · Apr 15, 2016 at 08:59 PM 0
Share

Yes, you absolutely can. If your button are positionned in a very "precise and not calculable" way, you can do :

 public Vector3[] positions;

Then complete the positions in the inspector. But if it's possible in your case, it would be more efficient to calculate the next position depending on the previous one. Imagine that your next button is always at x+10 from the previous button :

 public class Button$$anonymous$$aker : $$anonymous$$onoBehaviour {
  
      public Prefab button$$anonymous$$odel;
 
      public int buttonCount = 80;
 
      public Vector3 nextPos = Vector3.zero;
 
      public int spacingX = 10;
 
     void Start() 
     {
           for(int i=0; i<buttonCount; i++)
           {
                  CreateButton();
           }
     }
     
      public void CreateButton()
      {
            RectTransform myButton = Instantiate(button$$anonymous$$odel as RectTransform);
            myButton.SetParent(transform, false);
            myButton.localPosition = nextPos;
           nextPos.x += spacingX;
      }
  }


I am not sure if this completly answer your question, because I am not considering the "id" your talking about. Precise a bit more what you want to do if it's not helping ;)

avatar image VaderCmp Blue-Cut · Apr 18, 2016 at 04:45 PM 0
Share

Yes, this is what I was looking for.Thank you for the detailed answer! It helped me a lot!

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

Destroying assets is not permitted to avoid data loss. 0 Answers

Instantiated GameObjects scale down based on screen size. (Mobile) 0 Answers

My objects instantiate at a strange z coordinate 0 Answers

How to manipulate a variable of a prefab script (instantiated) while the game is runnning . 1 Answer

Distribute terrain in zones 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