- Home /
How can i fix this weird error
I have this script so when i click space the character whould reach up and take a sword off his back, and when i click space i get this wierd error:
NullReferenceException: Object reference not set to an instance of an object walker.Update () (at Assets/my scripts/walker.js:22)
this is my script:
if(Input.GetButtonDown("Jump")) { var animationComp : Animation[] = gameObject.GetComponentsInChildren(Animation) as Animation[]; for (var animComp : Animation in animationComp) { animComp.Play("grab sword");
}
}
whats wrong?
Answer by andrew 3 · Feb 05, 2011 at 04:09 AM
my first question dose he at least take the sword out or dose it just give you the error right of the bat i would say if he is not taking out the sword at all it is most likely since this is java script that java script dose not know it as jump it knows it as space bar try this and tell me your result i hope this helps.
when i press space (with my script, it works) i get the error, not right off the bat
then i wold say tat when he takes out the sword he dose not no how to put his sword away try anding after the grab sword to say if(Input.GetButtonDown("Jump")) { Animation[] animationComp = gameObject.GetComponentsInChildren(Animation) as Animation[];
foreach (Animation animComp in animationComp) {
animComp.Play("put back sword");
}
} try this and see what happens
how would that help because if it didn't know how to put it back, then there would be no way to put it back but he would still take it out. $$anonymous$$y problem is when i click space i get an error and he does nothing.
Answer by fireDude67 · Feb 05, 2011 at 04:11 AM
Your codes says var
it should be this:
if(Input.GetButtonDown("Jump"))
{
var animationComp : Animation[] = gameObject.GetComponentsInChildren(Animation);
for (animComp in animationComp)
{
animComp.Play("grab sword");
}
}
For a complete list of differences from C# to JS, see this post: http://answers.unity3d.com/questions/5507/what-are-the-syntax-differences-in-c-and-javascript
Edit: Oops, Don't Regularly use JavaScript, so I forgot to add var
keyword... :)
i got these errors:Assets/my scripts/walker.js(21,26): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/my scripts/walker.js(21,28): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/my scripts/walker.js(22,36): BCE0044: expecting ), found 'animComp'.
Assets/my scripts/walker.js(22,61): BCE0043: Unexpected token: ).
Assets/my scripts/walker.js(24,52): BCE0044: expecting :, found ';'.
Could you give it to me in JS because i Have a lot more to that script i just didn't post it because it did not have anything do do with this
i got these errors:
ArgumentException: You are not allowed to call get_gameObject when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function. walker..ctor () (at Assets/my scripts/walker.js:5)
Assets/my scripts/walker.js(24,31):BCE0043: Unexpected token: :.
Assets/my scripts/walker.js(24,32): UCE0001:';' expected. Insert a semicolon at the end.
/my scripts/walker.js(24,42): UCE0001:';' expected. Insert a semicolon at the end.
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object. 2 Answers
Instantiate giving a NullReferenceError 1 Answer
NullReferenceException: Object reference not set to an instance of an object 2 Answers
MoveTowards doesn't work after changing tag. 2 Answers
NullPointerException while changing a variable from one script in another 1 Answer