You should create 2 animations (from the Animation window): **Default** and **ChangeCondition** (or whatever makes sense). The **Default** animation will have your original/default sprite (drap and drop), and the **ChangeCondition** animation will have the new sprite (the sprite you're trying to load). Doing this will create 2 states in the Animator (with corresponding names).
In the next steps you need to:
1. Set **Default** (the state you created from the animation) as your default state (right-click and select from menu)
2. Create an integer from the Parameters menu (inside the Animator window). Call it **curScore** (or whatever relevant)
3. Make a transition from the **Default** state to the **ChangeCondition** state (right-click **Default** and select from menu)
4. Select the transition you just created (select the **Default** state and click on the transition inside of Transitions (from the Inspector window)
5. Setup the condition that will make the transition (between **Default** and **ChangeCondition**) to occur
- Inside of Conditions (from the
Inspector window), change Exit Time
to **curScore** (the integer created from
the Parameters window)
- The rest is self-explanatory. You obviously want it to equal 1, so
finish up the conditional statement
Now all you have to do is set the **curScore** integer (from the Parameters window) to manager.curScore...
// PRIVATE VARIABLES //
Animator animator;// Reference to the Animator Component
void Awake ()
{
// Set animator as a reference to the Animator Component on the GameObject
animator = GetComponent < Animator > ();
}
void Update ()
{
// Set the curScore integer (inside the Animator Component) equal to the curScore from the manager variable
animator.SetInteger ( "curScore", manager.curScore );
}
You should avoid using GameObject.Find and GetComponent inside Update() as it can bog down your game. If you must, find the GameObject in Awake() or Start() and create a reference to the Component and set that reference inside Awake() or Start().
Using this method saves you from checking a condition inside Update(), calling a function, grabbing a Component, and constantly loading a sprite everytime the condition is met. You could also change the sprite back to the **Default** state by creating a transition from **ChangeCondition** to **Default** and setting up the conditions.
P.S. I hope the C# syntax is correct. I use JavaScript and tried to convert it as best I could, sorry...
↧