Cannot access variable of another object's script

In my GUI script I want to set a variable’s value to true on another object’s script. But it’s not working … :confused:

Thanks for any help!

The error message:

error CS1061: Type UnityEngine.Object' does not contain a definition for doRotateL’ and no extension method doRotateL' of type UnityEngine.Object’ could be found (are you missing a using directive or an assembly reference?)

The GUI Script:

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour {

	public GUISkin mySkin;
	public GameObject bodies;
	private Object rotatorScript;

	void Start() {
	}

	void OnGUI() {

		GUI.skin = mySkin;

		GUILayout.BeginArea (new Rect (Screen.width*2/72, Screen.height/3, Screen.width*22/72, Screen.height/3));
		
		if(GUILayout.Button ("MoveBodyLeft", "button")) {

			rotatorScript = bodies.GetComponent("ClickRotator01");
			rotatorScript.doRotateL = true;
		}
		
		GUILayout.EndArea ();
	}
}

The other Object’s script:

using UnityEngine;
using System.Collections;

public class ClickRotator01 : MonoBehaviour {

public float rotationStepAngle = 30.0F;
public float timer = 10.0F;

private float rotationToAngle;
public bool doRotateL = true;

 	void Start () {
		float rotationToAngle = rotationStepAngle;
	}

	void Update () {
		Rotator();
	}
	
	void Rotator()
	{

		if(doRotateL){
			
			Quaternion newRotation = Quaternion.AngleAxis(rotationToAngle, Vector3.up);
			transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * timer);

			if (transform.rotation == newRotation){
				
				transform.rotation = newRotation;

				doRotateL = false;
				rotationToAngle += rotationStepAngle;
			}
		}
	}
}
  1. When your variable is of type Object you can only access method of Object.
  2. When you use the GetComponent(string type) overload of GetComponent() it will return a Component, however the generic form will return the specified type…

I suggest the following changes to GUITest:

//line 8 replacement:
//private Object rotatorScript;
private ClickRotator01 rotatorScript;

//line 21 replacement:
//rotatorScript = bodies.GetComponent("ClickRotator01");
rotatorScript = bodies.GetComponent<ClickRotator01>();