- Home /
Script finds everything but still i get "nullreference, object not set to an instance of an object"???
Hi everyone, this is my script, it prints everything correctly, so everything seems to be recognized and found, but when i want to play the animation of "Bird" i get a null reference exception "Object not set to an instance of an object", even though it prints the bird as gameobject bird! i'm very confused! how can that be? Maybe it has something to do with the fact, that the script is on a empty sprite and the object "Bird" is a child of the sprite? Has someone any idea? Thanks:)
var Baby : GameObject; var BabyPosz : float; var animationToolkit; var Bird;
function Start () {
Baby = GameObject.Find("baby");
Bird = GameObject.Find("bird");
animationToolkit = gameObject.GetComponent("tk2dAnimatedSprite");
}
function Update () {
BabyPosz = Baby.transform.position.z - 3.7;
transform.Translate(0,0,-0.03);
if(transform.position.z < BabyPosz){
print(Bird);
print(animationToolkit);
print(Baby);
print(BabyPosz);
Bird.animationToolkit.Play("Fly");
i work with animation toolkit and i have to get this component for playing animations:)
@Beriss and @$$anonymous$$imi: Don't post such requests as answers. Answers should answer the question above. Use comments if you need to clarify something. If you want to add something to your post you can edit it at anytime.
Answer by Owen-Reynolds · May 07, 2012 at 03:41 PM
You're mixing the Bird
and animationToolkit
variables in a non-working way. animationToolkit
is your animatedSprite component (not your child's.) You can use it as-is: animationToolkit.whatever
.
When you write Bird.animationToolkit
, you aren't using your animationToolket variable. Instead, you're using it as a magic word like Bird.transform
or Bird.rigidbody
. But animtoolkit isn't a magic words, so you get an error. C# would yell about it directly. Javascript tries to look it up and returns null.
You might mean to say: animationToolkit=Bird.GetComponent...
. But, GameObject.Find("bird")
isn't for finding children -- it finds a different top-level gameObject. Maybe you also want transform.Find("bird")
, to find your child bird.
Thank you very much for your answer! usually it works with bird.animationtoolkit.play but i never used it with a childobject before, so i didnt know that wouldnt work. Thanks again:)
Your answer

Follow this Question
Related Questions
Please Help!! NullReferenceException when clicking restore default pose 0 Answers
Getting variable from animation returns Null 0 Answers
Bug in Object reference ? 0 Answers
Null Reference - Help 0 Answers
Object reference not set to an instance of an object... 0 Answers