0

This is the screenshot of my assets/resources folder

I am currently having a hard time in loading a 3D object in the scene using a script in unity3d.

I made a gameobject in the scene wherein it will be populated by a 3D object.

Here's my code in loading the 3d, oh and I already added my 3d object in the resources folder.

object1 = Instantiate(Resources.Load("design2")) as GameObject;

What do you think am I doing wrong? Thank you!

Sarah
  • 135
  • 3
  • 19
  • Are you getting any errors? Are you sure "design2" is the correct path to your resource? – Serlite Feb 27 '17 at 15:31
  • No, but the output is just a 2D object – Sarah Feb 27 '17 at 15:36
  • perhaps `Resources.Load("design2") as Mesh` (or whatever type it is) will work better. `Resources.Load()` doesn't know what the object in your string path is, give it a type and try again! – Fredrik Schön Feb 27 '17 at 15:52
  • Are you trying to add the 3d object as a mesh component of the gameobject in the scene? Or are you trying to load the mesh as a new game object? – Mr.Bigglesworth Feb 27 '17 at 15:57
  • I am trying to load the 3D object as a new game object – Sarah Feb 27 '17 at 16:04
  • What type id the design2? Prefab or Image? Can you put a screenshot of it? – Programmer Feb 27 '17 at 16:09
  • already added the screenshot in my question @Programmer – Sarah Feb 27 '17 at 16:18
  • So you say there are no errors, but "the output is just a 2D object"...what do you mean by that? Can you show at runtime what kind of object gets generated? And how you can identify it as being, well, 2D vs 3D? (I'm having a hard time understanding how a 3D model would turn into a 2D object.) – Serlite Feb 27 '17 at 16:19
  • Realized that I didn't have 3D Object as one of my [answers](http://stackoverflow.com/a/41326276/3785314) on how to load resources. I just did. Check it out. Simply do this: `GameObject loadedObj = Resources.Load("design2") as GameObject; GameObject object1 = Instantiate(loadedObj) as GameObject;` ` if *design2* is a prefab. If it is a 3D file such as **.fbx** file, then do `Mesh model = Resources.Load("design2", typeof(Mesh)) as Mesh;`. Check that link for more info. – Programmer Feb 27 '17 at 16:45
  • Hi @Programmer, do you know how to set active a mesh? Because I only want to show the 3D object when a button is clicked – Sarah Feb 28 '17 at 15:01
  • After instantiating it, you can set active the GameObject that is connected to the mesh with `object1.SetActive(true);` and deactivate it with `object1.SetActive(false);`. You can also enable the `MeshRenderer` with `gameObject.GetComponent().enabled = true/false;` – Programmer Feb 28 '17 at 15:11
  • object1.SetActive (true); Mesh model = Resources.Load ("design2", typeof(Mesh)) as Mesh; gameObject.GetComponent ().enabled = true; ---- is this correct? – Sarah Feb 28 '17 at 15:23
  • What type of file is **design2**? When you select it it will show you the extension...Is it .prefab or fbx file? I need to know this to answer your last question. – Programmer Feb 28 '17 at 15:35
  • The file is design2.FBX – Sarah Feb 28 '17 at 15:37
  • You can load it as a GameObject. Please check **"3D Model (as GameObject)"** on the duplicated [answer](http://stackoverflow.com/questions/41326248/using-resources-folder-in-unity). After you instantiate it, you can do `object1.SetActive(true);`. – Programmer Feb 28 '17 at 15:50
  • Something like this: `GameObject loadedObj = Resources.Load("yourGameObject") as GameObject;` then this: `GameObject object1 = Instantiate(loadedObj) as GameObject;` followed by this; `object1.SetActive(true);`. – Programmer Feb 28 '17 at 15:51
  • 1
    Thank you so much! – Sarah Feb 28 '17 at 16:09
  • You are welcome. You can [accept](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work).... [Mr.Bigglesworth](http://stackoverflow.com/users/6904475/mr-bigglesworth) answer for his effort. His solution would have worked too but it's not necessary to do all those he mentioned. – Programmer Feb 28 '17 at 16:13
  • hi again, im sorry to bother you again @Programmer, i think i would be using 3D mesh such as fbx file.. but i do not know how to make it show only when the button is clicked – Sarah Feb 28 '17 at 16:57
  • My second to last comment should have worked for you. Please create new question and add the complete code that you currently have. Describe the problems with it and what you expect it to do then I will take a look at it. – Programmer Feb 28 '17 at 17:04

1 Answers1

0

Ok well if the 3d object resource is a an obj. or .fbx etc then the game won't be able to work out what you need from it. Instead instantiate a new GameObject then add a component of type Mesh, loading your 3d resource into the Mesh component.

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
private GameObject gameObject;
private Mesh myMesh;

void Start ()
{
    gameObject = new GameObject("Empty");
    gameObject.AddComponent<MeshFilter>();
    gameObject.AddComponent<MeshRenderer>();
    myMesh = (Mesh)Resources.Load("path",typeof(Mesh));
    GetComponent<MeshFilter>().mesh = mesh;
}

You will end up with what you seem to be going for. Code not tested but the concept is there.

Oh and the path at run-time is relative to the resources folder so unless it is in Resources folder you'll need to navigate to it. example:

 string path = "Models/mesh1";

Hope it helps.

Community
  • 1
  • 1
Mr.Bigglesworth
  • 924
  • 9
  • 22