-1
\$\begingroup\$

I would just like to ask if how can I generate a 2D rectangle shapes based on user's input. For example if he inputs 5, 5 rectangles will be created.

Also. I would like to put a integer attribute on the shape generated. (for example, Shape 1: Value 5f, Shape 2: Value 10f, Shape 3: Value 10f, etc...)

Any help will be appreciated! thank you!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Your question is very vague. How would the user input the number? Where will the shapes be added? What size? What have you tried? Please edit the question and add more details, and/or a mock-up of what you want. \$\endgroup\$
    – Vaillancourt
    Nov 15, 2017 at 11:46

1 Answer 1

-1
\$\begingroup\$

Because your question is not very detailed, there could be a number of ways to accomplish this.

If you are using buttons on the UI and Canvas as your methods of input, simply create buttons labelled 0-9 or whatever, and then onClick of these buttons, call the following function with the respective numbers input into them to specify how many rectangles you want, and at what size.

 public void createRects(int numOfRects, float value, float scaleX = 1, float scaleY = 1){

 //Iterate through number of rectangles
     for(int i = 0; i < numOfRects; i++){
     //Create new rectangle
     GameObject latestRect = Instantiate(rectanglePrefab, 
     transform.position, transform.rotation) as GameObject;
     //Set Scale and Position
     latestRect.transform.translate(5 * i,0,0);
     latestRect.transform.localScale += new Vector3(scaleX, scaleY, 1);
     //Name our rectangle according the value you wanted to hold. 
     //You can literally do anything with this instead.
     latestRect.gameObject.name = "Rectangle" + value.ToString();
     }
}

If you want to achieve it using a value that the User inputs via the Keyboard, then you would have to do the following instead:

void Update(){
   for ( int i = 0; i < 10; ++i )
   {
     if ( Input.GetKeyDown( "" + i ) )
      {
     for(int r = 0; r < i; r++){
       //Create new rectangle
     GameObject latestRect = Instantiate(rectanglePrefab, 
     transform.position, transform.rotation) as GameObject;
     //Set Scale and Position
     latestRect.transform.translate(5 * r,0,0);
     //Name our rectangle according the value you wanted to hold. 
     //You can literally do anything with this instead.
     latestRect.gameObject.name = "Rectangle" + r.ToString();
        }
       }
     }
    }
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can add a comment to the question and ask more details there :) \$\endgroup\$
    – Vaillancourt
    Nov 15, 2017 at 11:41
  • \$\begingroup\$ @AlexandreVaillancourt Obviously on a StackExchange site, we would encourage stronger questions, rather than stronger filtering in the comments to reach the correct question. I'm sure you simply forgot this. \$\endgroup\$
    – cmprogram
    Nov 15, 2017 at 11:43
  • \$\begingroup\$ No bother at all. As such, consider Accepting the Answer and Upvoting so other users know this is a reasonable answer to the given question. \$\endgroup\$
    – cmprogram
    Nov 15, 2017 at 16:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .