Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 crusherxman · Aug 20, 2013 at 07:15 PM · animationplaydestroyed

Playing animation when GameObject is destroyed

I've been working on a horror game and I've managed to create a "Key script" in order to pass through levels but I'm having a short problem by editing my script to play a animation once my "Key" is destroyed, so here's the script:

 var hasKey = false;
  
 function Update(){
 if(Input.GetKeyDown(KeyCode.E)){ //If E is pressed
 Debug.Log("Pressed");
 var hit : RaycastHit;
 if (Physics.Raycast (transform.position, transform.forward, hit, 100)) { // Sends out a raycast to check if something is in front of you
     Debug.Log("hit Something " + hit.transform.name);
 if(hit.transform.name.Equals("Key")){ // If the object in front of you has the tag Key
 hasKey = true;
 Destroy(hit.transform.root.gameObject);//Destroy Key Object
 }else if(hit.transform.name.Equals("Door")){ //Checks if the gameobject you're looking at has the tag Door
 if(hasKey)
 Debug.Log("HasKey");
 hit.transform.SendMessage("Unlock"); //Calls the function Unlock on the door
 }
 }
 }
 }

If there's more information that you guys might need, let me know in the answers below! :D

Thanks.

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

2 Replies

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

Answer by chelnok · Aug 20, 2013 at 07:35 PM

If the animation is inside the gameobject you are destroying, you need to play animation first, and destroy gameobject after animation is finished. Sure you have to check if animation is done. You can do that like this:

Here: http://docs.unity3d.com/Documentation/ScriptReference/Animation-isPlaying.html

Animation.isPlaying

var isPlaying: bool;

Description Are we playing any animations?

 // Plays an animation only if we are not playing it already.
 function OnMouseEnter() {
     if (!animation.isPlaying)
         animation.Play();
 }
Comment
Add comment · Show 5 · 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 crusherxman · Aug 21, 2013 at 01:59 PM 0
Share

Well my animation is somewhere else, on a GUI. Is there a different way to do this?

avatar image chelnok · Aug 21, 2013 at 03:10 PM 1
Share

Sure thing, many ways.. I suppose your animation is: http://docs.unity3d.com/Documentation/ScriptReference/Component-animation.html

one way: make var myAni:Transform to script you posted, drag gameobject that contains the animation, and just call myAni.animation.Play() first, and after that your destroy code.

avatar image crusherxman · Aug 23, 2013 at 07:33 PM 0
Share

Umm... noting seems to be happening. Should I give you my edited script? I think something is wrong :/

avatar image chelnok · Aug 24, 2013 at 12:31 AM 0
Share

what kind of animation are we talking about here anyway..?

avatar image crusherxman · Aug 30, 2013 at 10:11 PM 0
Share

Well, a GUI animation witch fades in/out.

avatar image
0

Answer by meat5000 · Aug 23, 2013 at 07:41 PM

OnDestroy()

Will this function work for you? I've not used it yet to be honest. Place your animation.play in this function and it should be performed when the object is destroyed. It cannot be used as a coroutine apparently.

Comment
Add comment · Show 3 · 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 chelnok · Aug 24, 2013 at 12:37 AM 0
Share

i really dunno if its just me ..but i'm starting to feel dizzy. What does that have to do anything? I think i have understood the very question totally wrong :)

edit: sorry.. i read the link was $$anonymous$$onoBehaviour.Destroy (i didnt even know there was OnDestroy)

avatar image meat5000 ♦ · Aug 24, 2013 at 12:42 AM 0
Share

OnDestroy(). A function that executes some code after you call Destroy(). In this case put OnDestroy() in '$$anonymous$$ey' script. When its destroyed OnDestroy contents will be executed.

avatar image meat5000 ♦ · Aug 24, 2013 at 12:47 AM 1
Share

I'm thinking that you are the first one to scroll down this far :P Surely this is the answer :D

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

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Run Sprint animation and stop it when button is released 1 Answer

Animation problem in script 0 Answers

Simple animation on game object doesn't play via animation.Play 1 Answer

problem not playing animation on keyDown 2 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