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. Dismiss Notice

Particles enabled/disabled OnTrigger- not working. What have I done?

Discussion in 'Scripting' started by SiMULOiD, Feb 6, 2016.

  1. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    121
    Can't understand why this won't work?
    Any ideas?

    Code (JavaScript):
    1. #pragma strict
    2.  
    3.  
    4. function OnTriggerEnter(trigger : Collider) {
    5.      if(trigger.gameObject.tag == "Water"){
    6.          GetComponent.<ParticleEmitter>().enabled = true;
    7.  
    8.      }
    9. }
    10. function OnTriggerExit(trigger : Collider) {
    11.      if(trigger.gameObject.tag == "Water"){
    12.         GetComponent.<ParticleEmitter>().enabled = false;
    13.      }
    14. }
     
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Um... Not enough information.
    Does collider have isTrigger set?
    Does object colliding it have rigidbody?
    Are you sure you're not trying to collide 2D against 3D colliders?
     
  3. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    121
    Hi, thanks for the reply.
    To answer your questions:
    • Colliders (both objects) have their triggers set.
    • Both have rigid bodies
    • Both are 3D colliders.

    This script is on a cube and I'm trying to have it trigger on another cube.
     
  4. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Let's start from another angle: does Debug.Log("Something"); work in OnTriggerEnter without any conditions?
     
  5. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    121
    Nothing seems to come up on the debugger.
    Also checked to see if objects were on different layers and if colliders are touching.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function Update ()
    4. {}
    5.  
    6. function OnTriggerEnter(trigger : Collider) {
    7.      if(trigger.gameObject.tag == "Water"){
    8.          GetComponent.<ParticleEmitter>().enabled = true;
    9.        Debug.Log("In water");
    10.      }
    11. }
    12. function OnTriggerExit(trigger : Collider) {
    13.      if(trigger.gameObject.tag == "Water"){
    14.         GetComponent.<ParticleEmitter>().enabled = false;
    15.          Debug.Log("Out of water");
    16.      }
    17. }
     
  6. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    121
    Got this line from debugger:

    MissingComponentException: There is no 'ParticleEmitter' attached to the "New Sprite" game object, but a script is trying to access it. You probably need to add a ParticleEmitter to the game object "New Sprite". Or your script needs to check if the component is attached before using it.
    WaterTrigger.OnTriggerExit (UnityEngine.Collider trigger) (at Assets/WaterTrigger.js:15)

    As far as I know they don't use emitters anymore, but particle systems. Is this right?
     
  7. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    All particles I used were ParticleSystem, yes. Afaik ParticleEmitter is old version of it(also called
    "legacy particles" by manual).
    But, well, it showed error that means at least trigger happened.
     
  8. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    You should use ParticleSystem. It is quite new and pretty awesome. Then you can simply create a particle system and enable it. Then use OnTriggerEnter and gameObject.SetActive(true) to enable your particle system.
     
  9. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    121
    Used legacy ellipsoid particle emitter and it turns it on and off correctly.
    Is there a way to do this with ParticleSystem?
     
  10. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Of course, but it'll take time to finetune it. So I'd say if emitters work as intended, leave them be(as stated in my signature). Especially if you need ellipsoid - there's no ellipsoid shape in ParticleSystem, only sphere and my attempts to simulate ellipse within 5 minutes failed.