I’m trying to write a function to reset multiple animations back to their beginning frame but am struggling to get any of the suggested methods to work.
I have the following script attached to 6 different buttons that each trigger the animation of a popup box. Each time a button(gameObject) is clicked I would like to reset the state of all popup boxes back to frame 1 and play the corresponding popup box animation.
#pragma strict
var animateObject:GameObject;
var o1:GameObject;
var o2:GameObject;
var o3:GameObject;
var o4:GameObject;
var o5:GameObject;
function openBox() {
Debug.Log("openBox Called");
animateObject.animation.Play("boxAnimOpen");
}
function resetBoxes() {
animateObject.animation.Stop("boxAnimOpen");
o1.animation.Stop("boxAnimOpen");
o2.animation.Stop("boxAnimOpen");
o3.animation.Stop("boxAnimOpen");
o4.animation.Stop("boxAnimOpen");
o5.animation.Stop("boxAnimOpen");
// I've also tried the following with no luck
// animateObject.SampleAnimation(animation.clip, 0);
}
function OnMouseDown(){
Debug.Log("mouse is down");
resetBoxes();
openBox();
}
I’ve tried “animation.Rewind” with no luck. I read in another forum post that animation.Rewind only works if it’s called while the animation is currently playing. Any other suggestions?
Are all the gameObjects assigned correctly in the inspector?
How is it not working? (is it throwing exceptions? do all the boxes stay open? no boxes open? etc…)
The script is attached to 6 buttons separately (ex. btnObject1, btnObject2, btnObject3…) and I have 6 game objects (popup boxes) connected to each button (total of 36 connections in the inspector).
The “animateObject” game object is the popup box that I want to open and “o1”,“o2”,“o3”,“o4”,“o5” are simply the other 5 objects that I want to rewind/reset.
If I click any of my button Objects the corresponding popup box animation plays no problem. The problem is when trying to click any of the other buttons that the previous popup boxes are stuck on the last frame.
The result is that after clicking all six buttons all six popup boxes are displayed when I only want to display one at a time.
Is there a simple way to create 6 separate OnMouseDown functions that are specific to each one of my buttons (btnObject1, btnObject2) and all contained within one script?
It would be ideal to only have to attach my 6 game objects to a single script. Currently I have my script attached to each button separately and have to make six connections in the inspector per button for a total of 36 connections.
In the case that I have 12 buttons and have to attach 12 game objects per each the number of connections grows exponentially to 144, this seems highly inefficient.
I would make a separate script altogether, and attach it to an Empty GameObject named “Boxes” something like:
//BoxesController.js
import System.Collections.Generic;
public var boxes: List.<GameObject>;
function Start() {
boxes = new List.<GameObject>();
}
function Reset() {
for (var box:GameObject in boxes) {
box.animation.Rewind("boxAnimOpen");
}
}
and then on each button object:
Edit:Fixed code to add GetComponent call
import System.Collections.Generic;
public var boxControl:BoxesController;
public var animateObject:GameObject;
function Start() {
if(boxControl == null){
boxControl = (BoxController)GameObject.Find ("Boxes").GetComponent(BoxController);
//this allows you to either set it in inspector or find it at the Start of run
}
boxControl.boxes.Add (animateObject);
}
function openBox() {
animateObject.animation.Play("boxAnimOpen");
}
function OnMouseDown(){
boxControl.Reset();
openBox();
}
standard disclaimer: this code was written in the web page editor, and not tested. Therefore report any bugs that may come up.
Am I correct in assuming that “boxes: list” is generated based on whatever game objects variables are connected to the “Boxes” game object in the inspector?
For example, in the case that I have 6 boxes would I still have to create 6 box variables in the “BoxesController.js” script and attach them to the “Boxes” empty game object in the inspector?
no. the code for the button has the line:boxControl.boxes.Add (animateObject);
so whatever animatedObject you assign to the button in the inspector, will be added automatically to the BoxController at runtime.
the “boxes: List.;” is basically setting up a dynamically sized array or list of GameObjects. This List will only hold GameObjects. (note the syntax and follow it exactly, in js you must have the period before the angle brackets, and you must import System.Collections.Generic) You can loop through them with “for(var … in …)”.
The Reset function now does not care how many boxes you have, it will loop through them all and rewind that animation. You could have 6 or 12 or 300,000 (although that would be a bit excessive).
The other script is attached to a button, the only real thing you must assign in the inspector is the box that that button controls.
I’m getting the following error in Unity:
Assets/Standard Assets (Mobile)/Scripts/BtnPopup.js(9,42): BCE0022: Cannot convert ‘UnityEngine.GameObject’ to ‘BoxesController’.
Do I need to replace “” with my game object variable?
Also, I’m not seeing “animateObject” as an option in the inspector to attach the game object to animate.
No is the type of thing that will be contained in that list.
animateObject is in the button script (line 4 of the second script above). Attach that script to each button. and then assign whatever object that button controls to that variable.
To be clear, you don’t do anything with the object named “Boxes” except attach the BoxController script to it. You don’t assign anything in the inspector. Don’t add variables to that script. It is completely stand alone and should need no other interaction.
On each of your buttons, attach the second script. In the inspector, you have to assign the box that it controls to animatedObject. Optionally you can assign the “Boxes” object, but it is not necessary.
This results in one assignment per button.
I just saw the error, sorry. Yes the line should be:
Also, the “animateObject” variable is not showing up in the inspector at all. Instead there is a “Boxes” option with a drop-down menu with a “Size” property with the default value of 0.
The “animateObject” variable is on the other script. The script you are looking at should only be on one object named “Boxes”. You don’t assign anything to this script in the inspector. You can look at it at runtime and see that it will have all the boxes in that list.
I attached the following script to an empty game object named “Boxes”:
//BoxesController.js
import System.Collections.Generic;
public var boxes: List.<GameObject>;
function Start() {
boxes = new List.<GameObject>();
}
function Reset() {
for (var box:GameObject in boxes) {
box.animation.Rewind("boxAnimOpen");
}
}
The following code is attached to a game object “BtnObject1” and intended to trigger the animation of a separate game object named “box1”. In theory I need to attach “box1” to the “animateObject” variable (this variable is not showing up in the inspector for “BtnObject1”.
import System.Collections.Generic;
public var boxControl:BoxesController;
public var animateObject:GameObject;
function Start() {
if(boxControl == null){
boxControl = GameObject.Find ("Boxes"); //or whatever you've named it
//this allows you to either set it in inspector or find it at the Start of run
}
boxControl.boxes.Add (animateObject);
}
function openBox() {
animateObject.animation.Play("boxAnimOpen");
}
function OnMouseDown(){
boxControl.Reset();
openBox();
}