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);

}
}

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);

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);