#if( GetComponent ) { ... }

1 messages · Page 1 of 1 (latest)

tall jay
#
            if(instigator.GetComponent<Experience>())
            {
                //code
            }````
code only runs if GetComponent is able to retrieve a component, correct?
frank grove
# tall jay ```cs if(instigator.GetComponent<Experience>()) { ...

If you want to test if the component was recieved, I recommend using TryGetComponent(); (if you want to use that component)

// try to get component (if it is successful, you "out" the component and can use that)
if (TryGetComponent(out Experience experience)) 
{
  // code
}

But I believe that that method could also work. If it doesn't, you can always check if the GetComponent returns null.