- Home /
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;
}
}
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...
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.