- Home /
This question was
closed Sep 01, 2018 at 09:34 AM by
srcsameer for the following reason:
The question is answered, right answer was accepted
how to create my fbx gameobject using script in the game
Hi! i am new to unity and i want to know... i have a script "GameObject go = GameObject.CreatePrimitive (PrimitiveType.Cube);" to create cube but i have imported another fbx model from a 3d software and instead of using cube i want to use my own fbx model can anyone tell me how to do it?
My Complete code is:
private void SpawnObj(Vector3 pos, Vector3 scale)
{
GameObject go = GameObject.CreatePrimitive (PrimitiveType.Cube);
go.transform.localPosition = pos;
go.transform.localScale = scale;
go.AddComponent<Rigidbody> ();
go.GetComponent<MeshRenderer> ().material = MatBl;
ColorMesh (go.GetComponent<MeshFilter> ().mesh);
}
Comment
Best Answer
Answer by devondyer · Feb 16, 2018 at 07:54 AM
You are going to want to look into instantiating your model here is the manual page on it.
The code to do that would be:
public GameObject myModel;
private void SpawnObj(Vector3 pos, Vector3 scale)
{
Instantiate(myModel, Transform.position, Transform.rotation);
myModel.transform.localPosition = pos;
myModel.transform.localScale = scale;
myModel.AddComponent<Rigidbody> ();
myModel.GetComponent<MeshRenderer> ().material = MatBl;
ColorMesh (myModel.GetComponent<MeshFilter> ().mesh);
}
You will have to drag and drop your model to the myModel variable that is now available in the inspector
@stretchy_tallman After using your code i am getting this error
Assets/Scripts/ShackUp.cs(85,33): error CS0120: An object reference is required to access non-static member `UnityEngine.Transform.position'
and
Assets/Scripts/ShackUp.cs(85,53): error CS0120: An object reference is required to access non-static member `UnityEngine.Transform.rotation'