Hi,
I was planning to change layer at runtime on far away BoxColliders, and MeshColliders to save some cpu cycles. There are some posts suggesting changing layer are less expensive compared to disable the collider component.
This is the code:
theCollider.gameObject.layer = 2;
But nothing happens, at least not in the Editor.
Can’t the layers be changed at runtime?
It should be OK. It will be changing the game object the collider is on, not the parent or child or rigidbody, are you sure you are looking at the right game object?
I just added this to a cube primitive
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Layers : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Start: " + gameObject.layer);
}
// Update is called once per frame
void Update()
{
Collider theCollider = GetComponent<Collider>();
theCollider.gameObject.layer = 20;
Debug.Log("Update: " + gameObject.layer);
}
}
And I can watch the layer change in both the inspector and the console data.