- Home /
Create objects with script?
I have a Model that I've made with 3ds max, and a little game, I want to create that object in the character's position when I press "T" for example, how would I do that?
Comment
Answer by whydoidoit · Sep 10, 2012 at 01:57 PM
You need to create a new game object and set its transform.position and .rotation to the character's position. Then you will need to add a MeshFilter component and a MeshRenderer component. The MeshFilter's shared mesh needs to be set to the object you created in Max.
public Mesh objectToCreate;
void Update()
{
if(Input.GetKeyDown("T"))
{
var gameObject = new GameObject("Some Name Here");
var meshFilter = gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
meshFilter.sharedMesh = objectToCreate;
gameObject.transform.position = transform.position;
gameObject.transform.rotation = transform.rotation;
}
}
Answer by hdgam3r · Sep 10, 2012 at 01:58 PM
First of all, you must make a Prefab that contains your 3DS Max model.
Then, you just need to instantiate it by using Instantiate in your script, you can even pass the position where you want your object to spawn.