Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice

[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,345
    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:
    15
    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");
     
unityunity