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;
}
}
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 :