Reputation: 11319
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
Upvotes: 3
Views: 2233
Reputation: 90630
GameObject.collider
was deprecated and removed in version 2019.1.0.
You can't use it for debugging anymore.
To check if there is a Collider
of any type use
var collider = child.GetComponent<Collider>();
to simply check if it exists you could also do
if(child.GetComponent<Collider>())
{
Debug.Log("Collider found");
}
again since Collider
(or better said Unity's type Object
from which it inherits) implements an implicit bool
operator which equals != null
.
A very nice way to add the component if it does not exist in a single line is therefore
Collider collider = child.GetComponent<Collider>() ? collider : child.gameObject.AddComponent<Collider>();
or even slightly shorter
Collider collider = child.GetComponent<Collider>() ?? child.gameObject.AddComponent<Collider>();
Note: Typed on smartphone so no warranty but I hope the idea gets clear.
Upvotes: 5
Reputation: 717
You can use Collider as it is the base class for all colliders.
private void AddDescendantsWithTag(Transform parent, List<GameObject> list)
{
foreach (Transform child in parent)
{
if (child.gameObject.GetComponent<MeshRenderer>() != null
&& child.gameObject.GetComponent<Collider>()) // You can modify this line according to your requirement.
{
list.Add(child.gameObject);
}
AddDescendantsWithTag(child, list);
}
}
Upvotes: 2