- Home /
How can i make this animation play only once? C#
Sorry to ask another question but I am a little stuck again... How can I make the On and Off animations in this flashlight script play only once? I know why it loops but I don't know how to make it stop, doesn't matter how. Thanks in advance.
using UnityEngine;
using System.Collections;
public class Flashlight : MonoBehaviour {
public GameObject Player;
Light flashlight;
bool on = false;
// Use this for initialization
void Start () {
flashlight = GetComponentInChildren<Light>();
}
// Update is called once per frame
void Update () {
Debug.Log(on);
// OnOff
if(on){
animation.Play("On"); // Make this play once
flashlight.light.enabled = true;
else if(!on)
animation.Play("Off"); // Make this play once
flashlight.light.enabled = false;
if(Input.GetButtonDown("Flashlight")){
on = !on;
}
}
}
Answer by fafase · Feb 25, 2014 at 11:25 AM
Have you tried this:
http://docs.unity3d.com/Documentation/ScriptReference/Animation-wrapMode.html
I haven't actually, what void does this go in? is it just
void Update(){
animation.wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
// Rest of Script...
}
How is what animation defined or is it for all animations used in the script?
You can make it happen once in the Start and it works on the animation clip:
void Start(){
animation["LightAnimation"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
}
Nah, that didnt work. $$anonymous$$aybe i did somthing wrong? Either way i appreciate the help.
using UnityEngine;
using System.Collections;
public class Flashlight : $$anonymous$$onoBehaviour {
public GameObject Player;
Light flashlight;
bool on = false;
// Use this for initialization
void Start () {
flashlight = GetComponentInChildren<Light>();
animation["On"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
animation["Off"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
}
// Update is called once per frame
void Update () {
Debug.Log(on);
// OnOff
if(on){
animation.Play("On"); // $$anonymous$$ake this play once
flashlight.light.enabled = true;
}
else if(!on){
animation.Play("Off"); // $$anonymous$$ake this play once
flashlight.light.enabled = false;
}
if(Input.GetButtonDown("Flashlight")){
on = !on;
}
}
}
$$anonymous$$aybe change for:
void Start(){
animation.wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;
}
The first one works on the animation clip, this one works on the animation player.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134651im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Transition Animations at end of current loop 0 Answers
Blender Animation Looping Delay Problem 2 Answers
Loop Animation In Script? 2 Answers
C# Make Child Not Animate Looped Animation 1 Answer
Using iTween for custom variables 3 Answers