- Home /
You are not allowed to call this function when declaring a variable.
Hi everybody, I followed what was written by "simple" in this post: http://answers.unity3d.com/questions/46576/dynamically-changing-the-character-motor-script.html
particularly, here is my code:
function Start () {
gameObject.GetComponent(CharacterMotor).movement.maxForwardSpeed = 10.0f;
}
but I get the following error by unity: UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function. UnityEngine.Component.get_gameObject ()
what's wrong?
Character$$anonymous$$otor character$$anonymous$$otor = gameObject.GetComponent(Character$$anonymous$$otor);
character$$anonymous$$otor.movement.maxForwardSpeed = 10.0f;
Have you tried splitting this expression up?
I'd imagine it isn't that line doing it - do you have a declaration somewhere else that might call something? Like outside the Start function?
Answer by Xtro · Aug 20, 2013 at 07:48 PM
is this a js or C# script?
if it's C#, do this
((CharacterMotor)gameObject.GetComponent<CharacterMotor>()).movement.maxForwardSpeed = 10.0f;
if js
((CharacterMotor)gameObject.GetComponent("CharacterMotor")).movement.maxForwardSpeed = 10.0f;
Answer by Skevil759 · Aug 20, 2013 at 08:12 PM
if I put the quotes I get this error: 'movement' is not a member of 'UnityEngine.Component' so I removed the quotes, closed Unity3d and reopened it. now it works.
oh. I forgot to cast the object. Sorry. Just updated it in my answer.
Your answer
Follow this Question
Related Questions
Problem accessing variable in javascript 1 Answer
problem with raycasting and functions 1 Answer
Problem with scaling a var over height 0 Answers
lerp with float problem!! 1 Answer
function OnTriggerEnter doesn't work 2 Answers