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 /
This question was closed May 05, 2017 at 09:43 AM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by codeguyross1 · Dec 05, 2012 at 08:14 PM · collidersizecubeprimitive

create primitive cube a runtime with specific size

This seems like a straight forward question that should be answered but I can't seem to find one example of how to create a primitive transform at runtime with a specific size. For example, I would like to create a transform with the size of 1.25,1.50,1.00.

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

3 Replies

  • Sort: 
avatar image
9
Best Answer

Answer by DaveA · Dec 05, 2012 at 08:22 PM

 var cube : GameObject  = GameObject.CreatePrimitive(PrimitiveType.Cube);
 cube.transform.position = Vector3(0, 0.5, 0);
 cube.transform.localScale = Vector3 (1.25, 1.5, 1);
     
Comment
Add comment · Show 6 · 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 codeguyross1 · Dec 05, 2012 at 09:50 PM 0
Share

Thanks for the quick response Dave! I thought scale multiplied rather than setting the size to a specific value.

avatar image Eric5h5 · Dec 05, 2012 at 09:54 PM 1
Share

It does. The default cube is 1 unit in size however.

avatar image codeguyross1 · Dec 05, 2012 at 09:56 PM 0
Share

Thanks guys. This post should give anyone else who searches this topic a concrete answer on how to set the size of a transform at runtime.

avatar image DaveA · Dec 06, 2012 at 12:50 AM 2
Share

Right. Careful with other objects. Default Unity Plane primitive, for example, is 10 units on edge, so these same numbers would make it 12.5 by 15 meters on edge.

avatar image Kasia_Wieciorek · Feb 11, 2015 at 02:05 AM 0
Share

What about spheres?

avatar image memBrain Kasia_Wieciorek · Jul 07, 2016 at 12:54 PM 0
Share

Yes, spheres too. (Sorry for necro-posting...hate unanswered questions.)

avatar image
0

Answer by blastoformity7 · Aug 01, 2016 at 02:20 PM

 GameObject[] CreatePrimitives(int totalGOs,Vector3 customScale,PrimitiveType pType){
         int i =0;
         GameObject[] setOf_GO = new GameObject[totalGOs];
         while(i<totalGOs){
             setOf_GO[i] = GameObject.CreatePrimitive(pType);
             setOf_GO[i].transform.localScale = customScale;
             i++;
                 }
         return setOf_GO;
 }
 
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 GearBreak · May 05, 2017 at 08:46 AM

codeguyross1

this should work: (call it by createPlane(1.25, 1.50, 1.00);)

 void createPlane(float Scale_x, float Scale_y, float Scale_z) {

         //setup a Vector3 to hold the scale variables
         Vector3 scale = new Vector3(Scale_x, Scale_y, Scale_z);
         //create and instantiate the plane object
         GameObject pl = GameObject.CreatePrimitive(PrimitiveType.Plane);
         //apply the scale variables previously saved in the Vector3
         pl.transform.localScale = scale;
     }
 

of course you can also choose any other Primitive object like

 GameObject pl = GameObject.CreatePrimitive(PrimitiveType.Cube);







Comment
Add comment · Show 2 · 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 Bunny83 · May 05, 2017 at 09:42 AM 1
Share

Some points:

  • This question is almost 5 years old, is already answered and has an accepted answer.

  • You use GameObjectUtility.SetParentAndAlign in your code which makes no sense because the GameObjectUtility is an editor class. So using it in a runtime method would prevent your game from being able to build. Editor classes can't be used at runtime. Also the way you use the method makes no sense either. It's ment to make the first object a child of the second and copy the layer and position of the parent. Since you pass the same object twice it makes no sense at all. An object can't be child of itself.

  • When you remove the just mentioned pointlless line of code your answer is exactly the same as the accepted answer. That makes your answer just a pointless necro-post.

avatar image GearBreak Bunny83 · Jun 27, 2018 at 03:46 PM 0
Share

I copied one of my editor extensions scripts (sorry for that) and now corrected the wrong line. I answered this old post because some unity beginners might search and find this thread and look for a simple c# (not javascript) version of it. So: it is NOT a pointless necro-post

Follow this Question

Answers Answers and Comments

15 People are following this question.

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

Related Questions

Something wrong with destroying script 0 Answers

Primitive Cube Texturing Help 3 Answers

MeshCollider vs. anyOtherCollider 1 Answer

Changing the size of a text object to only be as long as the letters 0 Answers

Dimensions of a Cube 2 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