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 Jeff00001 · Jul 28, 2014 at 07:17 PM · animationcoroutineienumerator

Play animation then destroy gameOject C# "SOLVED"

I'm trying to get the Death animation to play through and then destroy the gameObject but I seem to be doing something wrong. ( indicates in the code what I've been changing);

 void Start () {
     currentHitPoints = hitPoints;

     animation["Hit 2"].layer = 1;
     animation["Hit 2"].wrapMode = WrapMode.Once;

     **animation["Death"].wrapMode = WrapMode.Once;**

 }

 [RPC]
 public void TakeDamage(float amt) {
     currentHitPoints -= amt;

     if(currentHitPoints <= 0) {
         if( gameObject.tag == "Player" ) {

         }
         **StartCoroutine(Deathani());**
         Die();
     }

     if( gameObject.tag == "Player" ) {
         myScoreFlashFollow3D.Push(string.Format("-5"));
         animation.Play ("Hit 2");
     }
 }

 void Die() {
     if( GetComponent<PhotonView>().instantiationId == 0 ) {
         Destroy(gameObject);

     }
     else {
         if( GetComponent<PhotonView>().isMine ) {
             if( gameObject.tag == "Player" ) {
                 NetworkManager nm = GameObject.FindObjectOfType<NetworkManager>();

                 nm.standbyCamera.SetActive(true);
                 nm.respawnTimer = 10f;

             }

             PhotonNetwork.Destroy(gameObject);
     
         }
     }

 }

 **IEnumerator Deathani(){
     
     yield return new WaitForSeconds(transform.animation["Death"].length);**
 }
 
 void Update() {
     bar.ValueF = currentHitPoints / hitPoints;


 }

}

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
0

Answer by Hullu · Jul 28, 2014 at 07:51 PM

Have you tried having it like this:

 IEnumerator Die(){
     yield return new WaitForSeconds(transform.animation["Death"].length);
      if( GetComponent<PhotonView>().instantiationId == 0 ){
     Destroy(gameObject);
     //etc...




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 Jeff00001 · Jul 28, 2014 at 08:34 PM 0
Share

That doesn't work but thanks.

avatar image Hullu · Jul 28, 2014 at 09:21 PM 0
Share

Ok, can you tell what kind of error do you get?

avatar image
0

Answer by Jeff00001 · Jul 29, 2014 at 07:40 PM

The problem with using IENumerator or even Invoke to cause a delay over Photon just doesn't seem reliable, it works correctly for all players about 70% of the time.

I've solved the issue and done away with trying to use IENumerator or Invoke by instantiating another gameobject right before the network destroy and it works perfect.

I attached a component for self destruct & photonview, gave it a rigidbody with a sphere collider and used the following code to instantiate: (projectile will be the prefab you want to use)

At the top: public Rigidbody projectile;

 void Die() {
     if( GetComponent<PhotonView>().instantiationId == 0 ) {
         Destroy(gameObject);

     }
     else {
         if( GetComponent<PhotonView>().isMine ) {
             if( gameObject.tag == "Player" ) {
                 NetworkManager nm = GameObject.FindObjectOfType<NetworkManager>();

                 nm.standbyCamera.SetActive(true);
                 nm.respawnTimer = 10f;

             }
             PhotonNetwork.Instantiate(this.projectile.name, transform.position, transform.rotation, 0);
             PhotonNetwork.Destroy(gameObject);

         }
     }

 }


-----------------------------------Here is the self destruct------------------------------------

using UnityEngine; using System.Collections;

public class SelfDestruct : MonoBehaviour {

 public float selfDestructTime = 1.0f;
 
 void Update () {
     selfDestructTime -= Time.deltaTime;
     
     if(selfDestructTime <= 0) {
         
         PhotonView pv = GetComponent<PhotonView>();
         
         if(pv != null && pv.instantiationId!=0 ) {
             PhotonNetwork.Destroy(gameObject);
         }
         else {
             Destroy(gameObject);
         }
         
     }
 }

}

Hope this helps if your having the same issue.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Yield Wait for Seconds (A little help here) 2 Answers

Line of code skips randomly at times? 1 Answer

AnimationCurve as fixed time rather than portion of time? 1 Answer

Mecanim animation, playing once 1 Answer

WaitUntil and WaitWhile don't work 1 Answer


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