- Home /
accessing value of public variable from another file
Hi,
I have a pubic variable in a script(Pend_ball.cs) which is attached to child of a gameobject which is again a child of blank gameobject:
Pend_ball.cs
... speed = (transform.position - lastPosition).magnitude; ....
Now I want to access/print the value of "speed" in another script (KE.cs) which is also child of a blank gameObject:
KE.cs
public class KE : MonoBehaviour {
float ratio;
void Start(){
ratio = Time.deltaTime;
InvokeRepeating("ChangeScale",1f,1f);
}
void Update(){
float newVal = Mathf.Lerp(transform.localScale.y,speed,ratio);
transform.localScale = new Vector3(transform.localScale.x,newVal,transform.localScale.z);
} }
If you would be behind my back when I work you would spend your days laughing. I make that typos all them time.
That's a good thing :D Program$$anonymous$$g should be fun
Answer by Albz · Oct 26, 2013 at 11:53 AM
In KE.cs use
yourObject = GameObject.Find("parentGameobjectNameHere").GetComponent<Pend_ball>();
Then you will be able can access to the public variables of Pend_ball this way:
Debug.Log(yourObject.speed);
Thanks for help, for I get error:
error CS0029: Cannot implicitly convert type $$anonymous$$otionBall' to
UnityEngine.GameObject'
Answer by fafase · Oct 26, 2013 at 03:25 PM
On your accessor script, add a public (not pubic) reference to the Pend_ball script.
Then drag your sub/sub child into it.
There you go.
Now if you need to find it at runtime:
GameObject obj = GameObject.Find("The parent object");
Pend_ball pb = obj.GetComponentInChildren<Pend_ball>();
print(pb.speed);
Your answer
