-1

In Unity I have 2 GameObjects, a sphere and a capsule.

And I have a script attached to each.

Capsule script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CapsuleMesh : MonoBehaviour
{

    public Mesh capsuleMesh;

    void Awake()
    {
        capsuleMesh = GetComponent<MeshFilter>().mesh;
        Debug.Log(capsuleMesh);
    }
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}

Sphere script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class ChangeMesh : MonoBehaviour
    {

        Mesh mesh;

        void Awake()
        {
            mesh = GetComponent<MeshFilter>().mesh;
            Debug.Log(mesh);
        }
        // Start is called before the first frame update
        void Start()
        {
            mesh = capsuleMesh;

        }

        // Update is called once per frame
        void Update()
        {

        }
    }

The mesh = capsuleMesh here is giving me an error about "the name capsuleMesh does not exist in the current context".

I thought that making capsuleMesh public in the other script would make THIS script be able to access it without issue.

What am I doing wrong?

m0a
  • 1,005
  • 2
  • 15
  • 29

1 Answers1

2

capsuleMesh is a class variable defined in the CapsuleMesh class. It's not a global variable you can use everywhere. You need a reference to the instance of the CapsuleMesh class to retrieve the mesh stored in the capsuleMesh variable.

I've reworked your both scripts to make them work. I've spotted a flaw in your scripts. I guess ChangeMesh is meant to change the mesh of the gameObject? If so, you need to assign a new value to the meshFilter.mesh. Assigning a new reference to the mesh class variable is not enough (it would be pretty long to explain why)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CapsuleMesh : MonoBehaviour
{    
    public Mesh Mesh
    {
       get ; private set;
    }

    void Awake()
    {
        Mesh = GetComponent<MeshFilter>().mesh;
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeMesh : MonoBehaviour
{
    // Drag & drop in the inspector the gameObject holding the `CapsuleMesh` component
    public CapsuleMesh CapsuleMesh;

    private MeshFilter meshFilter;

    void Awake()
    {
        meshFilter = GetComponent<MeshFilter>();
    }

    void Start()
    {
        meshFilter.mesh = CapsuleMesh.Mesh;    
    }
}
Hellium
  • 7,206
  • 2
  • 19
  • 49
  • This is the solution. Thank you. I will follow this practice from now on. – m0a May 31 '19 at 11:44
  • 1
    Personally I would name the property `mesh` with a lower case M because its not a method (or rather, it doesn't look like one). But someone decided that because properties are secretly methods, they should follow the formatting rules for methods. This is particularly relevant because the class name and the property name are both `Mesh`, resulting in ambiguous usage later on. – Draco18s no longer trusts SE May 31 '19 at 15:37
  • 1
    I personally tend to use [Microsoft naming conventions](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members) : `Unlike other members, properties should be given noun phrase or adjective names. That is because a property refers to data, and the name of the property reflects that. PascalCasing is always used for property names.` but I totally agree with the problem of exact match of class name and property name. Microsoft suggest so though `CONSIDER giving a property the same name as its type.` – Hellium May 31 '19 at 16:13