Search Unity

[Solved] How to load default mesh?

Discussion in 'General Graphics' started by probitaille, Jun 2, 2015.

  1. probitaille

    probitaille

    Joined:
    May 5, 2015
    Posts:
    34
    I just want to get the default mesh plane by code and set it to a mesh filter:

    Code (CSharp):
    1.  
    2. MeshFilter meshFilter = myObject.AddComponent<MeshFilter> ();
    3. meshFilter.mesh = ((GameObject)Resources.Load("Library/unity default resources/Plane")).GetComponent<MeshFilter>().mesh;
    4.  
    I always get a nullReference error. What I'm doing wrong? What is the real path for default object?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There's no such thing since Resources.Load only uses items in Resources. Instead you can use GameObject.CreatePrimitive(PrimitiveType.Plane).

    --Eric
     
  3. probitaille

    probitaille

    Joined:
    May 5, 2015
    Posts:
    34
    Wow! Why didn't I Think of That Before?

    Thank you for your help. It's work perfectly.
     
    theANMATOR2b likes this.
  4. TheHobbyist

    TheHobbyist

    Joined:
    Nov 24, 2014
    Posts:
    19
    Is this still the best answer? I don't want a game object because it will cause a collision with the collider that is overlapping with where I want to put the mesh...

    I see "Cube" in the editor. Can't I load that via a script at runtime? Feels like I just can't get the path figured out.
     
  5. PolayToy

    PolayToy

    Joined:
    Oct 17, 2018
    Posts:
    1
    Resources.GetBuiltinResource will help, I get the builtin 'Cube' mesh in script here:
    Code (CSharp):
    1. // Create new mesh instantiate
    2. Mesh newLightSourceMesh;
    3. if (m_SelectedMesh != null)
    4.     newLightSourceMesh = Instantiate(m_SelectedMesh);
    5. else
    6.     newLightSourceMesh = Resources.GetBuiltinResource<Mesh>("Cube.fbx");
     
  6. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,905
    Also "Quad.fbx" for the 4-vertices quad.