Search Unity

My spawned obstacle stop working properly after a certain time

Discussion in 'Scripting' started by RafaelRiva, Jul 28, 2019.

  1. RafaelRiva

    RafaelRiva

    Joined:
    Sep 14, 2018
    Posts:
    6
    Since this is quite hard to explain, this is why I have recorded a video that show the bug:



    As you can see at 20 seconds my pillars stop receiving the trigger information, they aren't destroyed when hitted and also they don't deal damage anymore.

    Here is the code for the pilar:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using DarkTonic.PoolBoss;
    4. using UnityEngine;
    5.  
    6. public class ObstaclePilar_Infinity : MonoBehaviour
    7. {
    8.     public static InfinityGame GameManager;
    9.     public static GameObject game;
    10.     public GameObject PlayerP;
    11.     public GameObject ParticleHit;
    12.     Collider2D bodycollider;
    13.     Collider2D attackcollider;
    14.     Animator anim;
    15.     bool isDestroyed = false;
    16.     bool particlePlayed = false;
    17.  
    18.     // Start is called before the first frame update
    19.  
    20.     private void Awake() {
    21.         isDestroyed = false;
    22.     }
    23.  
    24.     void Start()
    25.     {
    26.         isDestroyed = false;
    27.         anim = GetComponent<Animator>();
    28.         game = GameObject.FindWithTag("GameController");
    29.         GameManager = game.gameObject.GetComponent<InfinityGame>();
    30.         PlayerP = GameManager.Player;
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.        
    37.     }
    38.  
    39.     public void DestroyThis()
    40.     {
    41.         PoolBoss.Despawn(transform, true);
    42.     }
    43.    
    44.     private void OnTriggerStay2D(Collider2D other)
    45.     {  
    46.         if (other == PlayerP.GetComponent<CapsuleCollider2D>() && !isDestroyed)
    47.         {
    48.         GameManager.PlayerHit();
    49.         }
    50.  
    51.         if (other == PlayerP.GetComponent<CircleCollider2D>() && PlayerP.GetComponent<CircleCollider2D>().isTrigger)
    52.         {
    53.             DestroyPilar();
    54.         }
    55.     }
    56.  
    57.     void DestroyPilar()
    58.     {
    59.         if (!isDestroyed)
    60.         {
    61.             SoundManager.Instance.PlayOneShot(SoundManager.Instance.PunchHit);
    62.             if (!particlePlayed)
    63.             {
    64.                 Instantiate(ParticleHit, transform.position, Quaternion.identity);  
    65.             }
    66.             isDestroyed = true;
    67.             anim.SetBool("Destroy", true);  
    68.         }
    69.     }
    70.  
    71. }
    72.  
    I am using a Pool add-on to handle pooling, I don't know if its related or not, because after hitting it im sending it back to the pool.

    Anyone had any idea where to look and approach the problem?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    With pooling, you need to reset all of the data on the object back to its initial state manually. I'm guessing that's as simple as setting isDestroyed back to false (and the animation data too) during OnEnable() but your pooling asset may work differently, so check the docs and see what method(s) are called when an object is added/removed from the pool.
     
    StarManta likes this.