try get component of type unity code example
Example 1: unity try get component
using UnityEngine;public class TryGetComponentExample : MonoBehaviour
{
void Start()
{
if (TryGetComponent(out HingeJoint hinge))
{
hinge.useSpring = false;
}
}
}
Example 2: unity trygetcomponent
Renderer rendererFound;
if (TryGetComponent(out rendererFound) == false)
{
Debug.LogError($"Error in {GetType()}: GameObject doesn't have a Renderer object");
return null;
}
float halfRadius = rendererFound.bounds.size.z / 2;