0
\$\begingroup\$

I want to use some particle effects into a bonus object that will be destroyed upon contact with the player object. This object as its name suggests will add a certain benefit for the player for a certain amount of time. I was following a tutorial on Youtube. The code from the Tutorial worked fine but there is one problem I was not able to solve: the explosion will not be destroyed after the object is collided with player and after the script is triggered because it seems that it was cloned into the scene. The instantiation created a clone of the effect's prefab I referenced into the script. After looking into the internet, I tried to affect the instantiation of the effect into a variable called clone and then destroyed it after the bonus is over but the problem was still present. I researched and I really did not obtain any answer.

bonus.cs //

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bonus : MonoBehaviour {

public float multiplier=2.0f;
public GameObject pickup_effect;
public float duration = 10;
private GameObject clone;

private void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("Player")) StartCoroutine( pickup(other));
}

IEnumerator pickup(Collider player)
{
    Debug.Log("You picked a bonus!");
    //spawn cool effect
    clone = Instantiate(pickup_effect, transform.position, transform.rotation);
    //apply effect yo the player
    // our.score_value += 10;
    player.transform.localScale *= multiplier;
    GetComponent<MeshRenderer>().enabled=false;
    GetComponent<Collider>().enabled = false;


    //wait x amount of seconds

    yield return new WaitForSeconds(duration);


    //remove the effect from theplayer
    player.transform.localScale /= multiplier;

    Destroy(gameObject);
    Destroy(clone,4f);


}


}

//

I tried disabling the "loop" option in the effect's particle system options but it gave the same results.

I disabled the "Play on Awake" option and the effect will not play at all.

The script is added directly to the object that is a simple 3D cube with MeshCollider, Mesh Renderer and Box collider.

The effect I used is a BigExplosionEffect taken from Unity Particle Pack 5.x from the Asset Store.

I would like to know what is exactly wrong? Is it probably related to the effect itself rather than the script? Or is there a far better solution?

\$\endgroup\$
  • \$\begingroup\$ Is anything printed to the console besides “You picked a bonus!”? Perhaps an error message? \$\endgroup\$ – Ed Marty Jun 8 at 15:55
  • \$\begingroup\$ The line Destroy(clone, 4f); will never execute. This is because you destroy the pickup object before it can wait 4 seconds to destroy the clone. \$\endgroup\$ – Sean Carey Jun 9 at 16:41
  • \$\begingroup\$ @Ed Marty There is no error message. I would have included them in my question if they appeared. \$\endgroup\$ – Khaled Adrani Jun 10 at 5:31
  • \$\begingroup\$ @SeanCarey I did understand that. I placed the destruction of the clone before the object but the same thing happened. However, I read that it might be due to the effect being a Chain effect. Should I go for another effect or is there a setting that changes it in the particle system window? \$\endgroup\$ – Khaled Adrani Jun 10 at 5:33
  • \$\begingroup\$ I can never be too sure. There are plenty of people who never even look at the console before asking questions. What @SeanCarey says seems correct though. May as well post an answer, no? \$\endgroup\$ – Ed Marty Jun 10 at 5:34
1
\$\begingroup\$

Here is the answer.

My mistake is that I destroy the clone after the destruction of the game object.

The initial code is fine. I just needed to call Destroy(clone,4) before I call Destroy(gameobject). Otherwise I am destroying the bonus object before the code to destroy the clone runs and therefore the clone will not be set to destroy after the four seconds.

Since it's a particle, I could also write some code right after instantiating the clone to destroy it after the duration of the particle effect (assuming it doesn't have child or chain effects that last longer than the main).

 // As quick example
 GameObject clone = Instantiate(effect, pos, rot);
 ParticleSystem.MainModule particle = clone.GetComponent<ParticleSystem>().main;
 Destroy(clone, particle.duration);

Well, I got this answer from a Unity Veteran from another forum and it worked out very well. I thought I had to reply to this question by myself for the benefit of everyone.

So my code will become like this:

``

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class bonus : MonoBehaviour {

    public float multiplier=2.0f;
    public GameObject pickup_effect;
    public float duration = 10;







    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player")) StartCoroutine( pickup(other));
    }

    IEnumerator pickup(Collider player)
    {
        Debug.Log("You picked a bonus!");
        //spawn cool effect
        GameObject clone = Instantiate(pickup_effect, transform.position, transform.rotation);
        ParticleSystem.MainModule particle = clone.GetComponent<ParticleSystem>().main;
        Destroy(clone, particle.duration);
        //apply effect yo the player

        player.transform.localScale *= multiplier;
        GetComponent<MeshRenderer>().enabled=false;
        GetComponent<Collider>().enabled = false;

        // our.score_value += 10;
        //remove the effect from theplayer

        yield return new WaitForSeconds(duration);

        //wait x amount of seconds

        player.transform.localScale /= multiplier;



        Destroy(gameObject);

    }


}

``

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.