Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_LUbQ6QyiZDNLzQ · Mar 18, 2019 at 02:59 PM · animationgameobject2d gamedestroyondestroy

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();
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_LUbQ6QyiZDNLzQ · Mar 18, 2019 at 04:37 PM 0
Share

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'));
         }
     }
 }





avatar image dargonknight · Mar 18, 2019 at 11:51 PM 0
Share

@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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

307 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

PLayer animation 0 Answers

Animation messes up if GameObject is destroyed 1 Answer

"Some objects were not cleaned up when closing the scene" 1 Answer

How to turn object into a particle system and back? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges