Accessing a value in a script attached to another game object

I am trying to access a variable stored in a script attached to another game object.

I seam to be able to access the script with this line Component theScript = OBJhit.GetComponent(opponentScript); However, when I try to change the value stored in that script (value) it turns red and I get the following error - Expression denotes a type where variable value or method group was expected.

Could anyone point me in the right direction?

if (Physics.Raycast (transform.position, fwd, out hit, 20)) {
    OBJhit = hit.transform.gameObject;
	    if (OBJhit.tag == "opponent") {
		    Component theScript = OBJhit.GetComponent(opponentScript);
		    theScript.value = true;
	    }
}

Make your theScript variable of type of your script as below:

opponentScript theScript  = OBJhit.GetComponent(opponentScript);
theScript.value = true;

Edit: Actually it should have been

opponentScript theScript  = OBJhit.GetComponent("opponentScript");

HarshadK had part of the answer right, but I am not sure the way you’re using GetComponent is the right one. (Not sure because I believe there’s 2 ways to use it)

The way I recommend using it is as a template function, here’s how to call it :

opponentScript theScript  = OBJhit.GetComponent<opponentScript>();
theScript.value = true;

Edit : After double checking, HarshadK had a working answer… I would still try this way and see if the result is any different.


re-Edit : third answer has me corrected : GetComponent(“opponentScript”) is the way to call it. (other than mine)

GetComponent() has three overloaded methods.

if you are using GetComponent(“string”), then you can write OBJhit.GetComponent(“opponentScript”);

or if you are using GetComponent(System.Type) then you have to write OBJhit.GetComponent<>();