Search code examples
c#androidunity-game-enginearcore

"'MeshCoIIider' is a type, which is not valid in the given context " error when trying to add a MeshCollider component to a mesh


I need to add a mesh collider component to every mesh that ArCore generates. However, it returns this error: "'MeshCollider' is a type, which is not valid in the given context."

I tried adding the component in the DetectedPlaneGenerator script. Here is the code:

namespace GoogleARCore.Examples.Common
{
    using System.Collections.Generic;
    using GoogleARCore;
    using UnityEngine;

    public class DetectedPlaneGenerator : MonoBehaviour
    {
        public GameObject DetectedPlanePrefab;
        private List<DetectedPlane> m_NewPlanes = new List<DetectedPlane>();
        public void Update()
        {
            // Check that motion tracking is tracking.
            if (Session.Status != SessionStatus.Tracking)
            {
                return;
            }
            for (int i = 0; i < m_NewPlanes.Count; i++)
            {
                GameObject planeObject = Instantiate(DetectedPlanePrefab, Vector3.zero, Quaternion.identity, transform);
                planeObject.GetComponent<DetectedPlaneVisualizer>().Initialize(m_NewPlanes[i]);
                ///Error here VVVVV
                planeObject.AddComponent(MeshCollider);
            }
        }
    }
}


Solution

  • See AddComponent

    Either use the generic form (recommended)

    planeObject.AddComponent<MeshCollider>();
    

    or you have to pass a type instead like

    planeObject.AddComponent(typeof(MeshCollider));