Double TryGetComponent gives an intellisense error

Using this code with Visual Studio 2019 gives an error

// rib gives the error
if (go.TryGetComponent(out Collider col) || go.TryGetComponent(out Rigidbody rib))
{
	if (col != null)
	{
		co = col;
	}
	if (rib != null)
	{
		rb = rib;
	}
}

// But if you try this "col" gives the error
if (go.TryGetComponent(out Rigidbody rib) || go.TryGetComponent(out Collider col))
{
	if (col != null)
	{
		co = col;
	}
	if (rib != null)
	{
		rb = rib;
	}
}

Is this correct as you can’t use double TryGetComponent within the same If statement, or is this a bug?

While you can do what Captain Pinapple suggested, Why do you actually check both TryGetComponent in a single if statement with an “or”, just to test the result of both individually again? This is completely redundant. Just do

if (go.TryGetComponent(out Collider col))
    co = col;

if(go.TryGetComponent(out Rigidbody rb))
{
    // do whatever you want with rb
}

Note: You have marked your Collider variable “co” with SerializeField, so this value is serialized and can be assigned in the inspector. However using TryGetComponent will always override what may have been serialized in that variable if the gameobject “go” has a Collider. So this whole bit of code looks confusing. If you want to set the collider through code, why serialize the field? If you want to implement some sort of fallback and only use TryGetComponent when you did not assign a collider in the inspector, you should do something like

if (co != null || go.TryGetComponent(out co))
{
    // do something with the collider co.
}

This first checks if the collider was assigned in the inspector and only if it was not assigned, it tries to get the collider from the gameobject “go”. If that fails as well, the code in the if body will not execute since we don’t have a Collider.

This is not a bug.

The error tells you everything in the shortest way possible what is going on: You try to use a variable which is not assigned.

Change this to

 Rigidbody rb = null;
 if( ..... || go.TryGetComponent(out rb))

and it will work. You might have to do the same for col as well.