Confusion with GetComponent(Script) vs GetComponent(Value)


I know how to use GetComponent fine, but a question with individual elements vs whole scripts.

I know that it probably takes longer for the game to get an entire script vs a singular element… but how would you go about setting the individual element so that it stays affected for more than the instance of the current script you’re working on?


PseudoScript 1:

public bool isGrounded;

PseudoScript 2:

void pseudoMethod()
bool grounding = GetComponent<Grounded>().isGrounded;
if (hitting)
grounding = true;
grounding = false;


The value fails to pass over to the original script, and I do not know why. Reason being is because:

Grouding grounding = GetComponent();
grounding.isGrounded = true; (works)

I know that you can do something like:

GetComponent < Grounding > ().isGrounded = true;

the thing is though, if i have 200 conditions, setting that every time will just clutter code.

Is there any other way to go about doing this? I don’t really feel comfortable getting an entire script just for one element.

Maybe I’m not using Google right, but I haven’t seen anything on this.

Thanks for any help!

It’s because you’re essentially copying the value into the variable grounding. What you intend to do is this:

Grounded grounded = GetComponent<Grounded>();
grounded.isGrounded = true/false;