- Home /
How to start animation after object is destroyed ?
So I have a dialogue and when it is finished it is destroyed. I want to start an animation after the dialogue is destroyed. So basically it is character that player talks to and when the dialogue is finished the character runs away. This running away is an animation that I want to play after the dialogue is destroyed and that it would be once.
I have tried and failed, I dont understand why. Example: //the player enters a 2dcollider on which the dialogue is activated //here I am trying to start animation when object is destroyed. Instead of it the animation plays on a loop when I enter the collider
void OnTriggerEnter2D(Collider2D other)
{
if(other.name =="Player")
{
theTextBox.ReloadScript(theText);
theTextBox.currentLine = startLine;
theTextBox.endAtLine = endLine;
theTextBox.EnableTextBox();
if (destroyWhenActivated)
{
Destroy(gameObject);
}
}
}
void OnDestroy()
{
animator2.SetBool("PlayRun", true);
}
void OnTriggerExit2D(Collider2D other)
{
theTextBox.DisableTextBox();
}
Answer by dargonknight · Mar 18, 2019 at 04:09 PM
first of all your telling the script to destroy the object if (destroyWhenActivated) is true as soon as the player triggers it which means if the bool was true it will destroy the object immediately causing OnDestroy to trigger the animator2 as for the looping, check if the "loop" checkbox is set to true on your animation. depending on how your game works you might want to launch the Destroy(gameObject) whenever the play press "continue" on your dialog or if the player do not control the text as soon as the last sentence is finished.
Thank you, I will focus on the lines then as I dont have continue button, maby you can give me heads up on how to activate animation when the currentLine/hits = 3; ?
I imagine it should be something like this, but it doesnt work
if(currentLine == 3)
{
animator2.SetBool("PlayRun", true);
}
This is the textbox manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextBox$$anonymous$$anager : $$anonymous$$onoBehaviour
{
public Animator animator2;
public GameObject textBox;
public Text theText;
public TextAsset textFile;
public string[] textLines;
public int currentLine;
public int endAtLine;
public CharacterController2D player;
public bool isActive;
// Start is called before the first frame update
void Start()
{
player = FindObjectOfType<CharacterController2D>();
if (textFile != null)
{
textLines = (textFile.text.Split('\n'));
}
if(endAtLine == 0)
{
endAtLine = textLines.Length - 1;
}
if(currentLine == 3)
{
animator2.SetBool("PlayRun", true); // This is where I placed this code
}
if (isActive)
{
EnableTextBox();
}
else
{
DisableTextBox();
}
}
private void Update()
{
if(!isActive)
{
return;
}
theText.text = textLines[currentLine];
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return))
{
currentLine += 1;
}
if(currentLine > endAtLine)
{
DisableTextBox();
}
}
public void EnableTextBox()
{
textBox.SetActive(true);
isActive = true;
}
public void DisableTextBox()
{
textBox.SetActive(false);
isActive = false;
}
public void ReloadScript(TextAsset theText)
{
if(theText != null)
{
textLines = new string[1];
textLines = (theText.text.Split('\n'));
}
}
}
@unity_LUbQ6QyiZDNLzQ while your if statement is correct i am unable to tell where do you actually run the code and switch lines but assu$$anonymous$$g that the function "nextLine()" is the function that would write the next line you need to check at the end of this function if the currentLine is 3 else you will need to continue writing. Though keep in $$anonymous$$d that player need enough time to read so it would be wise to wait a bit before switching lines in a Coroutine after that simply call the next line function