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

Spawn problem

Discussion in 'Scripting' started by simone9725, Aug 15, 2017.

  1. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Hi, I would like to check when my 3 bimbs exist and if they don't respawn them after 10 sec but there is something wrong.Any suggestion?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Bomber : MonoBehaviour {
    5.     public GameObject Punto1;
    6.     public GameObject Punto2;
    7.     public GameObject Punto3;
    8.     public GameObject Bomba1;
    9.     public GameObject Bomba2;
    10.     public GameObject Bomba3;
    11.     public float timer=10f;
    12.     // Use this for initialization
    13.     void Start () {
    14.      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if(!Bomba1)
    20.         {
    21.             timer -= Time.deltaTime;
    22.             if(timer<=0){
    23.                 Spawn ();
    24.             }
    25.         }
    26.  
    27.         if(!Bomba2)
    28.         {
    29.             timer -= Time.deltaTime;
    30.             if(timer<=0){
    31.                 Spawn1 ();
    32.             }
    33.         }
    34.         if(!Bomba3)
    35.         {
    36.             timer -= Time.deltaTime;
    37.             if(timer<=0){
    38.                 Spawn2 ();
    39.             }
    40.      
    41.         }
    42.     }
    43.     void Spawn()
    44.     {
    45.         Instantiate (Bomba1, Punto1.transform.position, transform.rotation);
    46.         //Invoke("Spawn", Random.Range (spawnMin, spawnMax));
    47.         timer=10f;
    48.     }
    49.     void Spawn1()
    50.     {
    51.         Instantiate (Bomba2, Punto2.transform.position, transform.rotation);
    52.         //Invoke("Spawn", Random.Range (spawnMin, spawnMax));
    53.         timer=10f;
    54.     }
    55.     void Spawn2()
    56.     {
    57.         Instantiate (Bomba2, Punto3.transform.position, transform.rotation);
    58.         //Invoke("Spawn", Random.Range (spawnMin, spawnMax));
    59.         timer=10f;
    60.     }
    61. }
     
  2. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Your code is not doing what you want it to do. You don't have any reference to the instantiated GameObject, Bomba1-2 appears to be the prefab reference so when you are running the if statements on your update they will never return null if you set them on the editor.

    So to fix this you need 3 new GameObject variables to save the instantiated Objects, then change your if statements to check for those GameObject not the prefab ones. That will fix it

    Code (CSharp):
    1. GameObject instantiatedGO;
    2. ...
    3.  
    4. Update()
    5. {
    6. if (!instantiatedGO)
    7. ...
    8. }
    9.  
    10. Spawn()
    11. {
    12. instantiatedGO = Instantiate(Bomba1, Punto1.transform.position, transform.rotation);
    13. ...
    14. }
    15. }
    Also you should consider using OOP for those functions there's no need to have 3 different functions that are doing the same thing.
     
    simone9725 likes this.
  3. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Thanks man I ve fixed it very fast and clear explanation :) have a nice day