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 have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Change Mesh Filter to a new custom mesh during runtime

Discussion in 'Scripting' started by Rtos, Jul 6, 2020.

  1. Rtos

    Rtos

    Joined:
    Jul 6, 2020
    Posts:
    2
    I have a growing plant, when it reach a certain maturity level I want to swap it's mesh for another more mature looking one. I can do that from the UI manually with no issue, but I can't find a way to make it with a script. That's my code right now, I think the problem comes from the Resources.Load. Right now this script will switch the mesh to None, and I'm not getting any error.

    Code (CSharp):
    1. gameObject.GetComponent<MeshFilter>().sharedMesh = Resources.Load<Mesh>("Assets/Meshes/plant_test_two");
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    It isn't finding the file you provided a string path to. Do you have a Resources/Assets/Meshes Directory?
     
  3. Rtos

    Rtos

    Joined:
    Jul 6, 2020
    Posts:
    2
    No I don't have a Resources folder, and if I try to create one at the root of the project it doesn't show in Unity. Also I don't understand why the API would have a Resources.Load command if a Resources folder isn't created by default (but Assets folder is...)?

    EDIT: Ok actually creating a Resources folder inside the Asset folder fixed it! Wasn't obvious to me. Thanks!