Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Particle Effects Not Working

Discussion in 'Getting Started' started by Faemoose, Oct 19, 2023.

  1. Faemoose

    Faemoose

    Joined:
    Aug 23, 2023
    Posts:
    4
    Hi, I'm trying to make it so that when these food obstacles touch the player, they will explode into a particle effect and disappear. I'm not getting any error messages, it just doesn't play the effect. I'm referencing between a few scripts and I'll also include screenshots of the editor where these variables are assigned. I tried assigning the script to the actual asset child object rather than the primitive parent object, but that made no difference.

    Code (CSharp):
    1. public class DestroyFood : MonoBehaviour
    2. {
    3.     public ParticleSystem explosionParticle;
    4.     public bool hitPlayer = PlayerController.beenHit;
    5.  
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.        
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         if (hitPlayer == true)
    16.         {
    17.             Explode();
    18.             hitPlayer = false;
    19.         }
    20.         //destroys when past the ground
    21.         if (transform.position.y < -2.0f)
    22.         {
    23.             Destroy(gameObject);
    24.         }
    25.     }
    26.  
    27.     public void Explode()
    28.     {
    29.         Instantiate(explosionParticle, transform.position, explosionParticle.transform.rotation);
    30.     }
    31. }
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     ...
    4.     private GameObject gameManager;
    5.     private GameManager gameManagerComponent;
    6.     private GameObject destroyFood;
    7.     public static bool beenHit;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         beenHit = false;
    13.         gameManager = GameObject.Find("Game Manager");
    14.         gameManagerComponent = gameManager.GetComponent<GameManager>();
    15. ...
    16.     }
    17.  
    18. ...
    19.  
    20. //Detects collisions with falling objects
    21.     public void OnTriggerEnter(Collider other)
    22.     {
    23.         if(other.gameObject.CompareTag("GoodFood"))
    24.         {
    25.             beenHit = true;
    26.             Debug.Log("Good Food caught!");
    27.             Destroy(other.gameObject);
    28.             gameManagerComponent.UpdateScore(1);
    29.         }
    30.         else if (other.gameObject.CompareTag("PowerUp"))
    31.         {
    32.             beenHit = true;
    33.             Debug.Log("Powerup gained!");
    34.             Destroy(other.gameObject);
    35.             StartCoroutine(PowerupCoroutine());
    36.         }
    37.         else if (other.gameObject.CompareTag("BadFood"))
    38.         {
    39.             beenHit = true;
    40.             Debug.Log("Bad Food caught!");
    41.             Destroy(other.gameObject);
    42.             gameManagerComponent.UpdateScore(-1);
    43.         }
    44.     }
    upload_2023-10-19_15-54-25.png

    I hope I'm posting this in the right place, I'm just very new to Unity and don't want to clog other forums with what may be a basic question. Any help is appreciated, thank you in advance!
     
  2. hanantahamada

    hanantahamada

    Joined:
    Oct 25, 2023
    Posts:
    1
    I got the same problem, hope there's solution about this.
     
  3. Reaktion

    Reaktion

    Joined:
    Nov 8, 2019
    Posts:
    53
    Hi,

    First thing that you want to do here is start debugging. You got it right by putting logs inside your PlayerController : I don't know in your post if you saw those logs are not. If not, it may be because code is simply not executed.

    If you are sure that your code is being executed and that you have no error, well there are two things to check :
    - If your particle is playing
    There’s a checkbox in the particle system editor labeled “Play on Awake” - if it's not checked, the particle is not playing when spawned.
    - If your particle is acting as you expect it to
    This is something you can easily check : just drop your particle system next to the location at where you're trying to spawn it from inside your code (or do a test close enough)
    Problems that could occur : particle is too small to be seen (or particle can be contained inside an object, so that you can't see it), particle spawn is too big (so particles are spawning off distance), or it can be something else

    This is something I would do in your case. There may be something more about it, so please post more infos about your problem :)
     
  4. Faemoose

    Faemoose

    Joined:
    Aug 23, 2023
    Posts:
    4
    @Reaktion Hi, thanks for the response. My particle system already has "Play on Awake" checked so that's not the issue. I also tried increasing the size of my effect and it didn't help. Can you explain what you mean by this: "drop your particle system next to the location at where you're trying to spawn it from inside your code (or do a test close enough)"? I dragged and dropped my particle system directly into my scene and it worked fine, but I can tell you may mean something else.
    Also, if it would be helpful, here are screenshots of the inspector of the system:
    upload_2023-11-5_18-37-58.png
    upload_2023-11-5_18-38-27.png
     
  5. Reaktion

    Reaktion

    Joined:
    Nov 8, 2019
    Posts:
    53
    Hi,

    That's what I was talking about : dropping your particle system into the scene to see if everything's work fine in the editor (and that it's not about the particle being not visible)

    Ok so if I understand your class
    DestroyFood
    correctly, the particle system will be played the next frame that food is being spawned, right ? Because of
    hitPlayer
    being true, entering the Update will trigger the particle system immediatly. Is that what you want ?
    To answer incoming questions : you should start debugging your code - by logging what you need to keep track of and to make sure that everything is called, at the right time. (just like what you did in your
    PlayerController
    , you should do it in your
    DestroyFood
    class)
    Always start debugging when your code is not doing what you want, and think about starting simpler maybe : just add a script that spawns your particle system at location (0,0,0) when press the spacebar for example. If it works, you'll know that particle is not a problem.
     
  6. Faemoose

    Faemoose

    Joined:
    Aug 23, 2023
    Posts:
    4
    Hi, thanks for the advice about debugging. I put a Debug.Log in the Explode() method in DestroyFood and it didn't log so I think Explode() is just not being executed. That's also my conclusion because I tried your other suggestion - writing a bit of code to just spawn a particle system when spacebar is pressed - and that worked perfectly so it's definitely not an issue with the particle.
    I switched around the code a bit so I was destroying the objects in DestroyFood, the same place as the explosion, instead of in PlayerController, but that didn't work either. I think it is very much linked to destroying the object that the particle was attached to.

    For this reason, I actually came up with a different solution: I decided to switch the particles over to the player since that object doesn't get destroyed. I deleted all the particle-related code in DestroyFood and made three new ParticleSystem variables in PlayerController, which I then assigned in the editor (three because I wanted different colors for different items). Then I tweaked my Instantiate() code to make them appear just above the player's head and placed it in the if statements for detecting collisions. The statements are nearly identical except for variable names so I'll just show one:
    Code (CSharp):
    1. if(other.gameObject.CompareTag("GoodFood"))
    2.         {
    3.             beenHit = true;
    4.             Debug.Log("Good Food caught!");
    5.             Destroy(other.gameObject);
    6.             Instantiate(goodExplosion, new Vector3(transform.position.x, 7, transform.position.z), goodExplosion.transform.rotation);
    7.             gameManagerComponent.UpdateScore(1);
    8.         }
    I realize this is a bit of a cop-out but in my case it works. Thank you again for helping to figure this out!
    Also, @hanantahamada you mentioned you had this problem too so I thought I'd @ you with this possible solution.
     
  7. Reaktion

    Reaktion

    Joined:
    Nov 8, 2019
    Posts:
    53
    Hi,

    You may want to do the exact same thing : listening to collision between food and player, but inside your Food.
    If you want the food to be responsible for the particle effect, implement the method
    OnTriggerEnter
    inside your
    DestroyFood
    class, make sure the Collider is the player and then instantiate the particle system.
    That's completely up to you : if you want the player to do that, keep it this way. And hey, code is working now, so you shouldn't fix something that isn't broken ! :)