C
C#2y ago
santijlm

Read and Set a variable from another script in unity

How can I edit a variable that is in another script linked to another object in unity?
4 Replies
Thinker
Thinker2y ago
Assuming you mean edit a property/field in another component, use var component = otherGameObject.GetComponent<TypeOfYourComponent>(); and then set whatever property/field you want using component.YourVariable = whatever;.
santijlm
santijlm2y ago
i mean a variable like a string but i think that could work
Thinker
Thinker2y ago
A "variable" is only something which can exist inside a method, outside a method it's called a field or a property.
santijlm
santijlm2y ago
oh, sorry then that's what i need