Changing a public varible from another script

I have a public variable called “moveSpeed” in a script called “CharacterMovement” and I need to edit the values in another script.

I tried:


charControl = GameObject.Find(“player”).GetComponent(CharacterMovement);

charControl.moveSpeed = 10;


But it gives me and error “Unknown identifier ‘CharacterMovement’” and “moveSped is not a member of ‘Object’” I am not sure what to do or how to fix it.

I think you should declare the charControl variable as a CharacterMovement type before assigning it.

private CharacterMovement charControl; (C#)
private var charControl : CharacterMovement (js)

Also maybe its ok to access script like that in js, but in C# I think you should write it like

GetComponent<CharacterMovement>();

Hope it helps :slight_smile:

Check if your scripts are actually attached, then double-check the naming, capital letters included.

this is an example you could use.

But by the looks of it, it should work.

is this unityScript or c#?

there are a couple of ways to do this:

charControl = GameObject.Find("player").GetComponent<CharacterMovement>();

charControl.moveSpeed = 10;

or

charControl = GameObject.Find("player").GetComponent(CharacterMovement) as CharacterMovement;

charControl.moveSpeed = 10;