accessing a script using a variable with getComponent, then accessing a variable inside that script

Hello. I’d like to know if something I am attempting is actually possible (C#):

`MonoBehaviour monobehaviour;
int theCapacity;

theCapacity = combineMag1.GetComponent ().currentCapacity;`

I’d like it to be so that i can assign a variable to access a specific script, and then use that variable to access a variable inside that script. If anyone knows how please help, thanks

Yes, simply use

YourComponentType comp = GetComponent<YourComponentType>();
comp.yourVariable = the value;

or all on one line if you’re sure the component is there.

GetComponent<YourComponentType>().yourVariable = the value;