- Home /
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 ... :/
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;
}
}
}
}
Answer by KellyThomas · Dec 26, 2013 at 12:33 PM
When your variable is of type
Object
you can only access method ofObject
.When you use the
GetComponent(string type)
overload of `GetComponent()` it will return aComponent
, 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>();
Thanks so much! @$$anonymous$$elly$$anonymous$$
Just for the record, one additional $$anonymous$$or edit was necessary to the change you made to line 5:
//line 21 replacement:
rotatorScript = bodies.GetComponent<ClickRotator01>();
Please post comments as comments not as answers!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Create a Button Scrollview 0 Answers
float error with screen.width/height 0 Answers