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

Question Instantiate Fxeffects (FXmanager.cs) in current destroyed object position.

Discussion in 'Scripting' started by Spark_23, Jul 29, 2022.

  1. Spark_23

    Spark_23

    Joined:
    May 17, 2018
    Posts:
    2
    Hi, I'm learning to develop games and programming in general, it's the first time I write here.

    I know that is easy to get position from prefab object to instantiate in that position sounds, fxeffects, etc.

    But I'm using a FxManager to avoid so many coroutines.

    I have a SpawnManager, this SpawnManager is controlling an array of Spawns in different positions, each Spawner is spawning an array of different prefab, each prefab have different sound clip and fx effect.
    These prefabs are enemies in movement, when an enemy is destroyed a DestroyEnemy.cs (a component of prefab) call to FxManager.cs to instantiate a explosionEffect (fx) and soundExplosion (clip).

    Currently, the FxManager is spawning the effects in 0,0,0 or in FxManager GameObject position if I use Instantiate(explosionEffect, gameObject.transform);

    How I can instantiate the effects in the position where enemy is destroyed?
    Thank you!

    Spawner.cs component in empty GameObject scenery
    Code (CSharp):
    1. IEnumerator SpawnCoroutine()
    2.     {
    3.         foreach (GameObject enemy in enemies)
    4.         {
    5.             Instantiate(enemy, transform.position, Quaternion.AngleAxis(180, Vector3.down));
    6.             yield return new WaitForSeconds(spawnShipInterval);
    7.         }
    8.     }
    DestroyEnemy.cs component in enemy prefab
    Code (CSharp):
    1. FxManager.fxManagerInstance.ShipExplosion(soundExplosion, explosionEffect);
    FxManager.cs component in empty GameObject scenery
    Code (CSharp):
    1.     public void ShipExplosion(AudioClip soundExplosion, GameObject explosionEffect)
    2.     {
    3.         Instantiate(explosionEffect);
    4.         source.PlayOneShot(soundExplosion);
    5.     }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Pass the position and rotation on where to create the effect as parameters of ShipExplosion.
     
  3. Spark_23

    Spark_23

    Joined:
    May 17, 2018
    Posts:
    2
    Thanks, I tried that too, but I think I'm doing something wrong because the effect is not shown.

    DestroyEnemy.cs
    Code (CSharp):
    1. Transform destroyPosition;
    2.  
    3. destroyPosition = gameObject.transform;
    4.             FxManager.fxManagerInstance.ShipExplosion(destroyPosition, soundExplosion, explosionEffect);
    FxManager.cs
    Code (CSharp):
    1.     public void ShipExplosion(Transform destroyPosition, AudioClip soundExplosion, GameObject explosionEffect)
    2.     {      
    3.         Instantiate(explosionEffect, destroyPosition);
    4.         source.PlayOneShot(soundExplosion);
    5.     }
    The code is right?