Home > Blockchain >  Unable to get the mesh from a GameObject
Unable to get the mesh from a GameObject

Time:07-19

I'm attempting to get the MeshFilter component on my platform GameObject to then save it's mesh in the platformMesh variable.

This is the code:

public class ObjectSelector : MonoBehaviour
{
    public GameObject platform;

    void Start()
    {
        private Mesh platformMesh = platform.GetComponent<MeshFilter>().mesh;
    }
}

I get the following error under the word "platform":

A field initializer cannot reference the non-static field, method, or property 'ObjectSelector.platform'.

I also get this error for the curly bracket right under Start():

} expected

I'm relatively new to unity, so I understood very little of the online resources I looked up concerning my situation.

CodePudding user response:

You got your field declaration syntax all confused:

public class ObjectSelector : MonoBehaviour
{
    public GameObject platform;
    Mesh platformMesh;

    void Start()
    {
        platformMesh = platform.GetComponent<MeshFilter>().mesh;
    }
}
  • Related