- Home /
Accessing a child's animation to play or stop it
OK, this is probably a nice simple question for someone out there, but it's causing me some problems. I have a switch on the base of a fan which can be turned on or off. This sends out a message to the fans switchscript, which flips the switch, and SHOULD start or stop the animation of the fan itself, which is a child of the fanbase object.
However, the code I'm using to do so is obviously incorrect (it lets me run the game, but when it should start or stop the animation, it gives the following error: NullReferenceException Fanbaseswitchscript.TurnedOff () (at Assets/Scripts/Fanbaseswitchscript.js:4))
Anyhow, here's my code. If someone could point out the error (I assume with syntax), it'd be much appreciated.
function TurnedOff () { transform.Find("Box002").transform.localPosition.x = -0.09; transform.Find("Fanhead").animation.Stop("Take 001"); }
function TurnedOn () { transform.Find("Box002").transform.localPosition.x = 0.09; transform.Find("Fanhead").animation.Play("Take 001"); }
When the switch is turned, it sends either a 'TurnedOff' or 'TurnedOn' (lol) message to this script. This then moves the box which represents the switch (to show its been switched), and should either play or stop the animation for the fan.
Edit: Here's a screenshot showing the fanhead objects inspector window:
Answer by Alienjesus · Apr 02, 2011 at 05:47 PM
Solved it, it was a really stupid mistake - I had childed Fanhead to fanbase whilst the game was playing, so it hadn't saved. Oops!
Answer by DaveA · Apr 01, 2011 at 07:05 PM
That's probably this line:
transform.Find("Fanhead").animation.Stop("Take 001");
and that error usually means something before the . is missing. Did you spell Fanhead correclty? FanHead maybe?
Then perhaps the object with Fanhead on it does not contain an 'animation' component? Can you post a screenshot of your editor window?
Solved it, it was a really stupid mistake - I had childed Fanhead to fanbase whilst the game was playing, so it hadn't saved. Oops!
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Children won't animate 2 Answers
[Solved] Animation destroying along with the GameObject its a child of 1 Answer
Problem with parent and child animation 1 Answer
Child Parent problem 1 Answer