0
\$\begingroup\$

In my game, I Instaniate several of the same prefabs for a group of player controlled characters. The prefab contains child objects with particle systems attached. For example one of the particle systems is a flame for the characters jet pack. The problem I'm having is that whenever one characters jet pack is in use, all of the Instantiated prefab particle systems are fired simultaneously.

void Start()
{
    mainThrust = this.GetComponentsInChildren<ParticleSystem> ();
    foreach (ParticleSystem jetPack in mainThrust)
    {
        jetPack.enableEmission = true;
        if (jetPack.isPlaying)
        {
            jetPack.Stop ();
        }
    }
}


private void MoveWithJetPack()
{
    foreach (ParticleSystem jetPack in mainThrust)
    {
        if (jetPack.isStopped)
        {
            jetPack.Play ();
        }

    }
}

private void killJetPack()
{
    foreach (ParticleSystem jetPack in mainThrust)
    {
        if (jetPack.isPlaying)
        {
            jetPack.Stop ();
        }

    }
}

How can I make this work for only the currently "jet packing" character. Also an explanation of why this code effects all of the characters that this script is attached to would be great. I thought that every script attached to a game object was considered a unique instance.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ I suggest to change this.GetComponentsInChildren<ParticleSystem>(); to gameObject.GetComponentsInChildren<ParticleSystem>();, because this keyword refers to the current instance of the class of your script, not GameObject that it's attached to. \$\endgroup\$
    – Exerion
    Sep 28, 2015 at 7:23
  • \$\begingroup\$ Excellent, I bet that will do it. I'll update once I've tested it. \$\endgroup\$
    – GeoJohn
    Sep 28, 2015 at 18:38
  • \$\begingroup\$ Unfortunately @Exerion suggestion didn't work. I've come up with a work around in which I test whether or not the particle system is the child of the currently selected character which seems to work fine, but doesn't seem like the most efficient way to do it. \$\endgroup\$
    – GeoJohn
    Oct 6, 2015 at 13:26
  • \$\begingroup\$ @GeoLohn, which object has this script attached to? Do you have some kind of "Holder", which contains your player controlled characters? \$\endgroup\$
    – Exerion
    Oct 6, 2015 at 14:23
  • \$\begingroup\$ @Exerion I have this script attached to all five of my player controlled character Game Objects. I also have an empty Game Object in my hierarchy called "game controller" that handles events throughout the game. So, for instance, a character that is about to move is the "currently selected" character in the "game controller" script. and a call is made as such when he needs to turn on his jetpack. currentSelection.GetComponent<jetPack_script>().MoveWithJetPack(); \$\endgroup\$
    – GeoJohn
    Oct 6, 2015 at 14:32

0

You must log in to answer this question.

Browse other questions tagged .