- Home /
Adding gravity and texture to newly added game objects
Hello everyone ! I am at the beginning phase of developing with Unity and I have this code to add a game object (simple cube) at the press of a button.
private void OnGUI()
{
if (GUI.Button(new Rect(50, 50, 25, 25), "+"))
{
GameObject newCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
int randNum = Random.Range(2, 5);
newCube.transform.position = new Vector3(randNum, randNum, randNum);
}
}
So my question is how to add gravity or even textures to these newly added cubes ?
Thanks in advance and sorry for any mistakes I might have slipped in, if any.
Dan.
Answer by eddyzy · Feb 19, 2014 at 08:59 PM
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.AddComponent.html
newCube.gameObject.AddComponent("Rigidbody");
for textures you`ll need a material
thanks ! exactly what I needed
Got any basic example for adding texture ? (material included). If not that's ok. The standard texture will do it.
Well you need a texture So
public Texture2D texture;
you assign it through the inspector
and then you do something like this
renderer.material.mainTexture = texture;
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$aterial.SetTexture.html
There`s a lot of things you can find out if you search a little.
Hint: if you don`t understand what a method/keyword does simply select it and press Ctrl + ' and it will open unity`s documentation on that method/keyword.
Thank you very much kind sir. I will do my best to take it on my own.