Search Unity

Why Destroy Call Doesn't Work After Other Lines of Code?

Discussion in 'Getting Started' started by TheCuriousHobbyist, Apr 29, 2019.

  1. TheCuriousHobbyist

    TheCuriousHobbyist

    Joined:
    Nov 13, 2018
    Posts:
    12
    For some reason, when my Destroy call is here:
    private void OnCollisionEnter(Collision collision)
    {
    Destroy (gameObject)
    if (collision.gameObject.CompareTag("Player"))
    {
    DisableRandom();
    player.maxCubes -= 1;
    }
    Debug.Log("Collided");
    GameObject obj = Instantiate(coll);
    if (CollectibleInside == true)
    {
    coll.transform.position = transform.position;
    }
    else { Destroy(coll); }
    }
    It destroys the object. However, when it's after everything else,
    private void OnCollisionEnter(Collision collision)
    {
    if (collision.gameObject.CompareTag("Player"))
    {
    DisableRandom();
    player.maxCubes -= 1;
    }
    Debug.Log("Collided");
    GameObject obj = Instantiate(coll);
    if (CollectibleInside == true)
    {
    coll.transform.position = transform.position;
    }
    else { Destroy(coll); }
    Destroy(gameObject)
    }

    it doesn't work. It doesn't seem like there would be anything, and I repeat ANYTHING, that would be causing this. I've also tried putting it in another function in the same script. The destroy call didn't work from there either. What. Is. Going. On? Anything that would possibly be causing this that anyone can pitch in with would be helpful, because this is just something else. It seems that you always have cryptic problems you can't solve, but this has no cause that I can think of. None.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It's going to be hard to help you, because (1) you posted your code without code tags, making it very hard to read; (2) you haven't said which of the several Destroy calls in this code you're talking about; (3) you haven't defined what "it doesn't work" means.
     
    Joe-Censored and Ryiah like this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're missing a semi-colon after all of your "Destroy(gameObject)" lines. You likely have console errors saying something to the effect of there are script errors you need to resolve before your code can compile. Also, your IDE like VS should be pointing out these errors.
     
    Ryiah likes this.