- Home /
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.
Answer by tiltaghe · Mar 06, 2013 at 01:32 PM
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 :)
Easier to type, but getting the same error "The name 'Character$$anonymous$$ovement' does not denote a valid type ('not found')"
Would it help to say that the Character$$anonymous$$ovement script is C#?
Answer by Shgoedt · Mar 06, 2013 at 01:30 PM
Check if your scripts are actually attached, then double-check the naming, capital letters included.
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html this is an example you could use.
But by the looks of it, it should work.
Just tried that, but seems like this is also giving me the same error. "The name 'Character$$anonymous$$ovement' does not denote a valid type ('not found')"
The script is attached and the spelling/capitalization seems correct.
Still causing problems.
Answer by PAEvenson · Mar 06, 2013 at 01:11 PM
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;
It's in Java Script. I tried the code but now it gives me another error, "The name 'Character$$anonymous$$ovement' does not denote a valid type ('not found')"
try:
charControl = GameObject.Find("player").GetComponent("Character$$anonymous$$ovement") as Character$$anonymous$$ovement;
charControl.moveSpeed = 10;
Like Shgoedt said, check your spelling. Also, do you have the script located in a special folder perhaps? Some folders get compiled at different times and if you are trying to access a script that hasnt been compiled yet, you will get an error.
Whttp://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.htmlW
Yes, they were in different folders. I changed it now but it still gives the same error. I checked the spelling and capitalization but they seem fine.