using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddColliders : MonoBehaviour
{
public List<GameObject> objectsToAddCollider = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
AddDescendantsWithTag(transform, objectsToAddCollider);
}
// Update is called once per frame
void Update()
{
}
private void AddDescendantsWithTag(Transform parent, List<GameObject> list)
{
foreach (Transform child in parent)
{
if (child.gameObject.GetComponent<MeshRenderer>() != null
&& child.gameObject.GetComponent<)
{
list.Add(child.gameObject);
}
AddDescendantsWithTag(child, list);
}
}
}
At this line I'm checking that there is a mesh renderer attached to the gameobject but how do I check if it don't have attached any collider type ? And then how to add a mesh collider to it ?
This is what I tried so far :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddColliders : MonoBehaviour
{
public List<GameObject> objectsToAddCollider = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
AddDescendantsWithTag(transform, objectsToAddCollider);
}
// Update is called once per frame
void Update()
{
}
private void AddDescendantsWithTag(Transform parent, List<GameObject> list)
{
foreach (Transform child in parent)
{
if (child.gameObject.GetComponent<MeshRenderer>() != null
&& child.gameObject.GetComponent<Collider>() == null)
{
child.gameObject.AddComponent<MeshCollider>();
list.Add(child.gameObject);
}
AddDescendantsWithTag(child, list);
}
}
}
But then in the end when adding a break point on the line :
AddDescendantsWithTag(transform, objectsToAddCollider);
I see that the gameobjects in the List objectsToAddCollider in the Collider this message :
collider = System.NotSupportedException: collider property has been deprecated