-1

I am creating an object named newTarget. Then I am loading an object from Resources folder which is fbx file.

fbx object has a mesh named as "Icosphere" in that case.

How can I use that mesh as newTarget's mesh?

I made some research but can not find a way to do it.

GameObject newTarget = new GameObject("NewTarget");
GameObject fbxObject = Resources.Load<GameObject>("Objects/Targets/target.fbx");
Hope
  • 2,096
  • 3
  • 23
  • 40
  • I'm confused what exactly you are trying to achieve ... if you load a mesh file already as a `GameObject` .. well then it already **IS** a `GameObject`. You can simply `Instantiate` it to make an instance clone of it ... in general afaik the path passed to `Load` should not contain the file extension see https://docs.unity3d.com/ScriptReference/Resources.Load.html – derHugo May 03 '23 at 11:58
  • if this is about runtime see https://stackoverflow.com/questions/51701361/how-can-i-load-fbx-file-in-unity-runtime -> you will need external tools for loading FBX meshes on runtime (= not in the Editor) – derHugo May 03 '23 at 12:01
  • I designed an object in Blender and want to load it to the scene using script. It it in fbx format in Resources folder.@derHugo – Hope May 03 '23 at 12:54
  • Also, I noticed that the fbx object has a name (file name) and under it there is mesh and there is a name for it too which blender gives when you creates an object. – Hope May 03 '23 at 12:58
  • As said: is this for runtime or the editor? In the editor you don't have to do anything actually .. you just drag your model asset into the scene or in a script load it via [`AssetDatabase.LoadAssetAtPath`](https://docs.unity3d.com/ScriptReference/AssetDatabase.LoadAssetAtPath.html) .. that's it -> you will get a `GameObject` with according hierarchy and meshes already applied. This works because Unity already has a built-in asset import processor to convert your FBX file into according `GameObject` structure – derHugo May 03 '23 at 15:00
  • You would probably rather want to do `var newtarget = Instantiate(fbxObject); newTarget.name = "NewTarget";` which creates a clone of the already existing FBX asset prefab – derHugo May 03 '23 at 15:02

1 Answers1

0

The only thing you should do is;

First drag drop your fbx export from Blender for example under Assets in Unity then;

GameObject fbxObject = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/newTarget.fbx");

Then you can set its position and parent like this;

    fbxObject.transform.SetParent(world.transform, true);


    float min = 0.1f;
    float max = 0.2f;


    Vector3 placemetPosition = new Vector3(Random.Range(-min, max),
                                            Random.Range(-min, max),
                                            Random.Range(-min, max));

    fbxObject.transform.position = placemetPosition;
Hope
  • 2,096
  • 3
  • 23
  • 40