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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Instantiated explosion prefab running immediately

Discussion in 'Scripting' started by shantanusingh994, Dec 28, 2015.

  1. shantanusingh994

    shantanusingh994

    Joined:
    Dec 26, 2015
    Posts:
    16
    Hi everyone, I want to initialize explosive prefab for a gameobject dynamically so I am using Instantiate(Resources.load("Explode Player Particle")) but this is making the explosion to occur as soon as it is getting instantiated. I want to run this explosive prefab when my player dies in the game. Also, I dont want to add the prefab from inspector I want to do it dynamically . How should I do this ?

    I am using this to initialize the explosive prefab - healthsc.explosionPrefab =(GameObject) Instantiate(Resources.Load("Explode Player Particle"));

    then on the Health(other script) I have a public explosionPrefab and I am running this code in this script -

    Code (CSharp):
    1. public class Health : MonoBehaviour {
    2.    
    3.     public enum deathAction {loadLevelWhenDead,doNothingWhenDead};
    4.    
    5.     public float healthPoints = 1f;
    6.     public float respawnHealthPoints = 1f;        //base health points
    7.    
    8.     public int numberOfLives = 1;                    //lives and variables for respawning
    9.     public bool isAlive = true;  
    10.  
    11.     public GameObject explosionPrefab;
    12.  
    13.     //public string explosionPrefabName;
    14.  
    15.     public deathAction onLivesGone = deathAction.doNothingWhenDead;
    16.    
    17.     public string LevelToLoad = "";
    18.    
    19.     private Vector3 respawnPosition;
    20.     private Quaternion respawnRotation;
    21.    
    22.  
    23.     // Use this for initialization
    24.     void Start ()
    25.     {
    26.         // store initial position as respawn location
    27.         respawnPosition = transform.position;
    28.         respawnRotation = transform.rotation;
    29.        
    30.         if (LevelToLoad=="") // default to current scene
    31.         {
    32.             LevelToLoad = Application.loadedLevelName;
    33.         }
    34.     }
    35.    
    36.     // Update is called once per frame
    37.     void Update ()
    38.     {
    39.         if (healthPoints <= 0) {                // if the object is 'dead'
    40.             numberOfLives--;                    // decrement # of lives, update lives GUI
    41.            
    42.             if (explosionPrefab!=null) {
    43.                 Instantiate (explosionPrefab, transform.position, Quaternion.identity);
    44.             }
    45.    
    Note - I haven't posted the full code for convenience.
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    Has your explosion particle got PlayOnAwake set to true? If so you need to set it false.
     
  3. shantanusingh994

    shantanusingh994

    Joined:
    Dec 26, 2015
    Posts:
    16
    After setting PlayOnAwake to false. Its not playing at the start but its not playing when player dies too
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Make that:
    Code (csharp):
    1. healthsc.explosionPrefab = Resources.Load<GameObject>("Explode Player Particle");
    You should Instantiate it only when the player dies (and it appears that you are doing that part correctly, on line 43 of the pasted code). Until then, you just want a reference to the prefab, which is what Resources.Load gives you.
     
  5. shantanusingh994

    shantanusingh994

    Joined:
    Dec 26, 2015
    Posts:
    16
    Thanks StarManta one line change did the trick