Access variable in parent script C#

Hi,
so this is driving me nuts because it should be so simple.
How do I change a variable in the parent script from the childs script?
Thanks a lot!

This should work:

this.transform.parent.GetComponent<YourparentScript>().yourVariable = newValue;

But the variable must be public.

Though you will not want to do it every frame (GetComponent once in Start()), conceptually you can:

parentscriptclass myParent = transform.parent.GetComponent<parentscriptclass>();
myParent.somepublicmember = "new value";

You can try this.

MyParentScript myScript = this.GetComponentInParent ();

Now you can call any public function or variable.,

myScript.myVariable = whatever // set variable

myScript.myFucntion() // runs function

thank you for this saved me time also :slight_smile: