- Home /
How do I trigger an animation when a certain number appears
Hi, I need a script that triggers an animation when i have collected 14 boxes from this script...
using UnityEngine;
using System.Collections;
public class Boxes : MonoBehaviour {
private int collected = 0; //Set up a variable to store how many you've collected
public AudioClip collectedSound; //This is the sound that will play after you collect one
public GUIStyle mystyle;
//This is the text that displayed how many you've collected in the top left corner
void OnGUI(){
GUI.Label(new Rect(40, 10, 500, 500),"Boxes:" + collected + "/14", mystyle);
}
private void OnTriggerEnter(Collider other){ //Checks to see if you've collided with another object
if(other.CompareTag("collectable")){ //checks to see if this object is tagged with "collectable"
audio.PlayOneShot(collectedSound); //plays the sound assigned to collectedSound
collected++; //adds a count of +1 to the collected variable
Destroy (other.gameObject);}}}
Please Help!
Answer by ExTheSea · Mar 31, 2013 at 12:53 PM
public class Boxes : MonoBehaviour {
private int collected = 0; //Set up a variable to store how many you've collected
public Transform AnimateGO; //Drag GameObject which should be animated in here.
public AudioClip collectedSound; //This is the sound that will play after you collect one
public GUIStyle mystyle;
//This is the text that displayed how many you've collected in the top left corner
void OnGUI(){
GUI.Label(new Rect(40, 10, 500, 500),"Boxes:" + collected + "/14", mystyle);
}
private void OnTriggerEnter(Collider other){ //Checks to see if you've collided with another object
if(other.CompareTag("collectable")){ //checks to see if this object is tagged with "collectable"
audio.PlayOneShot(collectedSound); //plays the sound assigned to collectedSound
collected++; //adds a count of +1 to the collected variable
if(collected == 14){
//Play Animation here like for example:
AnimateGO.animation.Play("AnimationName"); //Change AnimationName to the name of your animation.
}
Destroy (other.gameObject);}}}
Answer by thz343 · Mar 31, 2013 at 03:15 PM
@ExTheSea I tried the script on the object i wanted to animate but it didn't work. could you get it so there is a GameObject variable in the script. i tried adding it in myself but it didn't work.
I edited my answer so that there is a Transform variable where you can drag the gameobject which should be animated in.
Btw. What you posted as an answer should have been a comment to my answer. Answers are mainly used for answers to the main question not as response to someone who posted an answer.
Btw. If your question is solved please accept an answer or close the question.