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

Accessing a particle system via script [SOLVED]

Discussion in 'Scripting' started by QuinnWinters, Mar 3, 2015.

  1. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    I'm attempting to access a particle system via script. Looking at the documentation it says all that's required is something like "particleSystem.Emit(5);", but when I use anything of the sort I get errors. I can assign a type of ParticleSystem to a variable, but I can't seem to find any way of adding a particle system to a variable in the inspector.

    With the variable in the following example it gives an error of "Language Feature Not Implemented." Without the variable, with just ParticleSystem.Emit, it gives the same error.
    Code (JavaScript):
    1. private var myParticleSystem : ParticleSystem;
    2.  
    3. function OnCollisionEnter (other : Collision) {
    4.  
    5.     if (other.gameObject.tag == "MyObject") {
    6.         myParticleSystem.Emit = false;
    7.     }
    8.  
    9. }
    First off, what am I doing wrong here?

    Second, what I'm actually trying to do is make the particle emmiter stop emitting but I don't want the particles to just disappear all at once. I want the particles it's already created to fade out naturally. Would I get that result from turning off the emitter?

    Any help is much appreciated!
     
  2. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    230
    Deeeds likes this.
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    Thanks, that did the trick. Now that I look again I see that in the documentation on particleSystem, not sure how I missed it before.
     
  4. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    I've got another similar question and thought I'd just reuse this thread instead of starting a new one....

    I'm now trying to access a particle system that's a child of another object. The other object will sometimes have the child and sometimes won't, so I've got it checking childCount to see if there are children because it gets an error if I try to run GetComponentsInChildren when there are no children. It sees that there are children just fine, but when it goes to check if there is a particle system on the children (which in this case there always is) it doesn't ever see the particle system. The debug.log I stuck in there never fires. I've been going in circles on this one for an hour or so trying all kinds of things, but with no luck. Any ideas how I can successfully get access to the particle system on the child object?
    Code (JavaScript):
    1.     if (transform.childCount > 0) {
    2.         var particleSystems : Component[];
    3.         particleSystems = GetComponentsInChildren (ParticleSystem);
    4.         for (var particles : ParticleSystem in particleSystems) {
    5.             Debug.Log ("Found a particle system");
    6.             particles.enableEmission = true;
    7.         }
    8.     }
    Edit T+45 minutes later: I thought perhaps it was just the particle system that it couldn't find, so I wrote a script and added that to the child and tried to access the script via GetComponentsInChildren, and I'm not able to access it either. Once again it finds that there are children but doesn't find the instance of the script. Perhaps I'm somehow using GetComponentsInChildren wrong, though I've never had issues with it in the past.
    Code (JavaScript):
    1.         if (transform.childCount > 0) {
    2.             var returnScript : Component[];
    3.             returnScript = this.GetComponentsInChildren (ReturnParticlesToPool);
    4.             for (var returnScriptInstance : ReturnParticlesToPool in returnScript) {
    5.                 Debug.Log ("Found an instance of the return script");
    6.                 returnScriptInstance.ReturnObjects ();
    7.             }
    8.         }
    Edit T+1 hour 50 minutes later: I came up with a workaround that doesn't involve using GetComponentsInChildren, but instead uses a system of public and private booleans. I would still like to learn what I was doing wrong here though, if anyone would care to enlighten me.
     
    Last edited: Mar 3, 2015
    Deeeds likes this.
  5. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    230
    Code (CSharp):
    1. foreach (child:Transform in transform){
    2.     if(!child.gameObject.activeInHierarchy) continue; //If child object is not active, ignore it (optional)
    3.     child.gameObject.particleSystem.enableEmission = true;
    4. }
    Give that a go. I don't like that GetComponentsInChildren function as it seems vague/confusing. The above code simply loops through the children in the transform (if any), skips ones that are not active, and runs code on the others. Nice and simple.

    You don't even have to use any 'GetComponent' functions really, since the particleSystem property is already part of gameObject.
     
    Deeeds and QuinnWinters like this.
  6. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    Aah, thank you so much. That solved a major problem for me. Much appreciated. I'm glad you put that comment in there about how if the child object is not active, as that was where my problem was located. I was deactivating the object before trying to turn the particles back on in it.
     
    Juice-Tin likes this.