gwapings69
2
Q:

unity try get component

Renderer rendererFound;
//If TryGetComponent doesn't find a Renderer on this, it returns false
//If a Renderer is found, it return true AND places the renderer that was 
//found into 'rendererFound'
if (TryGetComponent(out rendererFound) == false) 
{
  Debug.LogError($"Error in {GetType()}: GameObject doesn't have a Renderer object");
  return null;
}
//Use 'rendererFound' asif component was found
float halfRadius = rendererFound.bounds.size.z / 2;
1
using UnityEngine;public class TryGetComponentExample : MonoBehaviour
{
    void Start()
    {
        if (TryGetComponent(out HingeJoint hinge))
        {
            hinge.useSpring = false;
        }
    }
}
0

New to Communities?

Join the community