- Home /
Accessing a variable that has a unique class
this is a portion of my animation code:
function Start () {
var motor : CharacterMotor = gameObject.GetComponent(CharacterMotor);
animation["idle"].wrapMode = WrapMode.Loop;
animation["jump_pose"].wrapMode = WrapMode.ClampForever;
animation["walk"].wrapMode = WrapMode.Loop;
animation["run"].wrapMode = WrapMode.Loop;
}
function LateUpdate () {
charVelocity = motor.movement.velocity;
CalculateState();
ApplyState();
}
My problem is in the line "charVelocity = motor.movement.velocity;" it says Unknown identifier 'motor' (BCE0005).
Movement is a variable with a class of its own and I am trying to access the velocity part of it after the code has run in the other script.
Try this
var motor : Character$$anonymous$$otor = gameObject.GetComponent<Character$$anonymous$$otor>();
I use C# but I think JS uses that; basically the problem is that motor isn't getting the reference it should. I'm assu$$anonymous$$g the Character$$anonymous$$otor component is on this same gameobject.
You could also just declare motor as public and drag/drop the Character$$anonymous$$otor object into it in the Editor.
@getyour411 In UnityScript generics are actually done using a dot:
GetComponent.<Character$$anonymous$$otor>()
But it's actually suggested to not use the generic GetComponent
method in UnityScript and rely on the one the OP is using. There's nothing wrong with the getting of the Character$$anonymous$$otor
component.
'motor' is local to whatever it was declared in, if you create the variable inside of a function it is local to that function and can't be accessed outside of it. You'll have to store the data in a variable outside of any functions of type Character$$anonymous$$otor/Component.
Answer by oatsbarley · Jan 25, 2014 at 11:56 PM
You declare motor
in Start()
and then reference it inside LateUpdate()
which is outside of its scope, hence the "Unknown Identifier" error - it doesn't exist there. You need to declare motor
as a class variable, so that all methods of the class can access it.
Answer by Benproductions1 · Jan 25, 2014 at 11:57 PM
Your problem is, that you're trying to access a variable that doesn't exist. On line 2 in Start
you declare a variable motor
of type CharacterMotor
. Since the variable is declared inside of a function, it will only exist during that function and will be freed once the function returns. It doesn't exist anymore when you try to access it in the Update
function.
To fix this, you should make the motor
variable a private variable on your MonoBehaviour
class. Class variables (fields) exist as long as the object they are part of does.
class SCRIPTNAME extends MonoBehaviour {
private var motor:CharacterMotor;
function Awake() {
#This is the fastest method of GetComponent
motor = GetComponent(typeof(CharacterMotor)) as CharacterMotor;
}
function Update() {
#Yay, it exists here too!!
Debug.Log(motor);
}
}
Scripts in Unityscript are already classes that extend $$anonymous$$onoBehaviour. :)
But they don't have to be. And they can contain other classes, so in order to remove confusion and to stay consistant it's better to declare the class explicitly. At least thats my opinion on the matter. It also re$$anonymous$$ds new people, that they are really just writing classes, not some magical script thing that works as a $$anonymous$$onoBehaviour
magically...
They actually do have to be; every script is a class that extends $$anonymous$$onoBehaviour. You can put other classes in there that don't, but you really are writing a magical script thing that extends $$anonymous$$onoBehaviour since that's how Unityscript works.
If you write a class in the script with the same name as the file it's in, it will replace the magical crap that the UnityScript translator adds in. (Yes, I've done it many times before)
Your answer
Follow this Question
Related Questions
Issues with trying to change a variable in another script 1 Answer
How could you access a script of varying name? 5 Answers
CharacterMotor problem (Js) 1 Answer
Need to use 2 different language scripts. 1 Answer
Static Variable Problem 1 Answer