Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Cloned gameObjects not set active

Discussion in '2D' started by Zemox, Jul 11, 2020.

  1. Zemox

    Zemox

    Joined:
    May 6, 2020
    Posts:
    9
    Code (CSharp):
    1. public class SpikeDamage : MonoBehaviour
    2. {
    3.     public float speed = 2.0f;
    4.  
    5.     void Start()
    6.     {
    7.         gameObject.SetActive(false);
    8.     }
    9.     void Update()
    10.     {
    11.         transform.position += -transform.up * speed * Time.deltaTime;
    12.         gameObject.SetActive(true);
    13.     }
    14.  
    15.     void OnTriggerEnter2D(Collider2D other)
    16.     {
    17.         if (other.tag == "SpikesDamage")
    18.         {
    19.             Destroy(gameObject);
    20.         }
    21.     }
    22. }
    23.  
    24.  
    25.  
    Code (CSharp):
    1. public class CreateSpikeDamage : MonoBehaviour
    2. {
    3.     public float destroy = 5.0f;
    4.  
    5.     public GameObject spikeDamage;
    6.     public Transform spikeDamagePos;
    7.  
    8.     private void OnTriggerEnter2D(Collider2D other)
    9.     {
    10.         if (other.CompareTag("Player"))
    11.         {
    12.             spikeDamage.SetActive(true);
    13.             Destroy(spikeDamage, destroy);
    14.             Instantiate(spikeDamage, transform.position, transform.rotation);
    15.         }
    16.     }
    17. }
    18.  
    I want to spawn a gameObject every time I walk onto a trigger, when I walk onto it at first it does what I want but when I walk onto it again it spawns the clones but they are not active. What am I doing wrong? Thanks!
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,010
    this might be a problem:

    spikeDamage.SetActive(true);
    Destroy(spikeDamage, destroy); <--- soon you will instantiate destroyed object
    Instantiate(spikeDamage, transform.position, transform.rotation);
     
  3. Zemox

    Zemox

    Joined:
    May 6, 2020
    Posts:
    9
    How could I fix this ? Maybe make another function with the destroy code but what kind of function could I add ?
     
  4. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,010
    First of all tell us what you want to achieve.
    What is SpikeDamage (also I suggest naming your MonoBehaviours with Component in name, like SpikeDamageComponent)
     
  5. Zemox

    Zemox

    Joined:
    May 6, 2020
    Posts:
    9
    I basically just want to spawn a gameObject when the player goes into a collider that Is trigger that kills the player and after some time the gameObject gets destroyed those two things work. The only thing that doesn’t work is when I go onto the collider again it doesn’t work the gameObject doesn’t spawn again.
     
    Last edited: Jul 11, 2020
  6. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,010
    First of all a dont know why are you even messing around with gameObject.setActive In SapikeDamage.
    The second thing i dont undesratnad why are you doing this.
    1. spikeDamage.SetActive(true);
    2. Destroy(spikeDamage, destroy);
    3. Instantiate(spikeDamage, transform.position, transform.rotation);
    You basically destroy your prefab. What for are you doing this ?
     
  7. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    The thing is that you are destroying the prefab instead of destroying the instantiated one.

    So it works the first time but the second time you try the prefab is destroyed aka null and when you try to copy it you are just copying a null.

    I suggest
    GameObject go = Instantiate(spikeDamage, transform.position, transform.rotation);

    Then destroy go instead.