- Home /
How to create instance from model without creating a clone?
I'm creating an GameObject instance from a fbx model in the editor with the following code snippet:
static void CreateObject(ObjectInfo info)
{
GameObject at;
if (GameObject.Find("AtTree") == null)
{
at = new GameObject("AtTree");
at.transform.position = new Vector3(0, 0, 0);
}
else
{
at = GameObject.Find("AtTree");
}
GameObject root = new GameObject(info.Name);
root.transform.parent = at.transform;
root.AddComponent<Attachable>();
GameObject tmp = AssetDatabase.LoadAssetAtPath<GameObject>(FILEDIR + info.Name + ".fbx");
GameObject mod = Instantiate(tmp, root.transform, true);
mod.transform.position = new Vector3(0, 0, 0);
mod.transform.localScale = new Vector3(100, 100, 100);
Instantiate(mod);
}
This creates the GameObject I want (blue), but also an additional object (red) The blue object is the one I want to keep, but I don't want the red one. How can I remove the red one or create the blue one without creating the red one at all?
What is the purpose of the final call to Instantiate()? It looks like this is what's creating the second object, so if you don't want to create it, try just omitting that line.
Answer by tanoshimi · Sep 03, 2017 at 08:48 PM
You're calling Instantiate() twice, so you'll get two instances. If you only want one, delete Line 25.
Answer by ichgebdirgleichinvalid · Sep 04, 2017 at 04:37 PM
Oh, now I see the failure. Thank you this works perfectly.
Your answer
Follow this Question
Related Questions
How to not instantiate an object if there is another object there? 1 Answer
Instantiated Prefab doesn't find main camera 2 Answers
How can I check if an instantiated object collides with another instantiated object? 1 Answer
Piece of code creates unwanted GameObjects in scene 3 Answers
C# Why do I have to use .AddComponent and not "new (ClassName)"? 1 Answer