Search Unity

  1. We want you to join us at GDC this year! Take a peek at all of the events we will be hosting during the week of GDC.
    Dismiss Notice
  2. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  5. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice

Problem with loading meshes at runtime!

Discussion in 'Scripting' started by WiebeVanAken, Sep 13, 2017.

  1. WiebeVanAken

    WiebeVanAken

    Joined:
    Apr 9, 2017
    Posts:
    15
    Hello reader!

    I have a problem with loading meshes at runtime!
    I'm trying to instantiate an object through a class.

    Here is my code where I load the mesh itself:

    Code (CSharp):
    1. Mesh mesh = (Mesh)Resources.Load("Assets/Models/cubemesh", typeof(Mesh));
    And the assign code:
    mesh is the input to this function
    Code (CSharp):
    1.  
    2. public void InstantiateObject(Mesh mesh)
    3.     {
    4.         Debug.Log(mesh);
    5.         if(mesh != null)
    6.         {
    7.             Debug.Log("Mesh exists");
    8.         }
    9.         else
    10.         {
    11.             Debug.Log("Mesh does not exist");
    12.         }
    13.         //Create NPC object
    14.         npcObject = new GameObject(name);
    15.         MeshFilter meshFilter = npcObject.AddComponent<MeshFilter>();
    16.         meshFilter.mesh = mesh;
    17.        
    18.     }
    19.  
    This code it's output is: "Mesh does not exist". But I clearly input a mesh. Please help me!
     
  2. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,231
    If you want to use Resources.Load, the asset needs to be in a folder called "Resources", and you call Load using only the path from within that Resources folder, for example "Models/cubemesh".
     
  4. WiebeVanAken

    WiebeVanAken

    Joined:
    Apr 9, 2017
    Posts:
    15
    Thank you, makeshiftwings. I changed around the path of the object. It is now linked to the resources folder.
     
  5. WiebeVanAken

    WiebeVanAken

    Joined:
    Apr 9, 2017
    Posts:
    15
    @SlyRipper
    The mesh is already saved as an prefab. As I try to import the mesh as an gameobject I get this error:
    NullReferenceExeption: Object reference not set to an instance of an object.

    Here is my code:

    Code (CSharp):
    1. private Mesh mesh;
    2.  
    3. GameObject meshObject = (GameObject)Resources.Load("Models/cubemesh", typeof(GameObject));
    4.         mesh = meshObject.GetComponent<MeshFilter>().mesh; //The IDE says that the error is located right here
    5.  
    6. InstantiateObject(mesh);
    7.  
    8. public void InstantiateObject(Mesh mesh)
    9.     {
    10.  
    11.         //Create NPC object
    12.         npcObject = new GameObject(name);
    13.         MeshFilter meshFilter = this.npcObject.AddComponent<MeshFilter>();
    14.         meshFilter.mesh = mesh;
    15.     }
    16.  
    This is my code.​
     
    Last edited: Sep 15, 2017
  6. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,231
  7. WiebeVanAken

    WiebeVanAken

    Joined:
    Apr 9, 2017
    Posts:
    15
    I read the document. I already figured that part out. And loading it as an gameobject. But I got a new problem now. I get a nullreferenceexecption.
     
  8. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    Like makeshiftwings mentioned.. Do you have an "Resources" folder? Is there a "Models" folder inside of it? outside the Resources folder the method is not usable. [​IMG]
     
  9. WiebeVanAken

    WiebeVanAken

    Joined:
    Apr 9, 2017
    Posts:
    15
    I got it. Yes, I already had an resources folder because all the YT videos I watched and the document on the reference told me I had to use a resource folder.