So I have a parent object called “Animate” and it has two children. “AnimateCircle” and “DownButton”.
Now for the explanation of my dilemma:
Animate.js:
This class looks at DownButton.cs and checks to see if a variable is true.
public static var downbutton : DownButton;
function Start()
{
//find children and look at appropriate classes
downbutton = GetComponentInChildren(DownButton);
}
function Update ()
{
playanimation = false;
if(downbutton.WasDownPressed())
{
playanimation = true;
}
}
function PlayAnim()
{
return playanimation;
}
DownButton.cs: This class returns a boolean value which is only true when an object is clicked on.
public class DownButton : MonoBehaviour
{
public static bool downPressed = false;
void Update ()
{
//NOTE HERE
}
void OnMouseDown()
{
downPressed = true;
}
void OnMouseUp()
{
downPressed = false;
}
public bool WasDownPressed()
{
return downPressed;
}
}
AnimateCircle.js: This class looks to the parent “Animate” to see if a value is true so it can play an animation.
public static var animate : Animate;
function Start()
{
animate = transform.parent.GetComponent(Animate);
}
function Update ()
{
if(animate.PlayAnim())
{
animation.Play("animationone");
}
}
Okay, so what you currently see works, but it needs to be altered. Currently if I hold the button down on the object the object will continue to animate, this is because in DownButton.cs the OnMouseDown() function is where the boolean is being set to true. What I’ve done before is make the boolean true at OnMouseUp() and then set the boolean to false in the Update function, where I wrote “//NOTE HERE”. However this does not work in this case because I have a message being sent from child to parent to child. By the time the message is delivered the update function has ran again and has set the boolean back to false. I was wondering if someone had a way to make this work, or a different method I can think of that might work would be to pass the function value straight from child to child, though I don’t know the syntax for that. As you can see in Animate.js I’m using GetComponentInChildren to look at the child value in DownButton.cs and then Looking up at the parent from AnimateCircle.js to Animate.js using transform.parent.GetComponent(Animate).