- Home /
The question is answered, right answer was accepted
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!
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);
Thanks for the quick response Dave! I thought scale multiplied rather than setting the size to a specific value.
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.
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.
Yes, spheres too. (Sorry for necro-posting...hate unanswered questions.)
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;
}
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);
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.
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
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