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

NEED A LITTLE HELP!

Discussion in 'Scripting' started by froi0603, Jul 2, 2017.

  1. froi0603

    froi0603

    Joined:
    Jun 12, 2017
    Posts:
    23
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Bomb : MonoBehaviour {
    6.     [SerializeField]
    7.     private GameObject[] prefab;
    8.     private bool gameStarted = false;
    9.     private HatController hats;
    10.     private BoxCollider2D wall;
    11.     private float x1, x2;
    12.     void Awake () {
    13.         wall = GetComponent<BoxCollider2D> ();
    14.         x1 = transform.position.x - wall.bounds.size.x / 2f;
    15.         x2 = transform.position.x + wall.bounds.size.x / 2f;
    16.    
    17.     }
    18.     void Start()
    19.     {
    20.         StartCoroutine (SpawnPrefab (1f));
    21.         hats = GameObject.FindGameObjectWithTag ("Player").GetComponent<HatController> ();
    22.     }
    23.     IEnumerator SpawnPrefab(float time) {
    24.         yield return new WaitForSecondsRealtime (time);
    25.  
    26.  
    27.         Vector3 temp = transform.position;
    28.         temp.x = Random.Range (x1, x2);
    29.  
    30.         Instantiate (prefab[Random.Range(0, 1)], temp, Quaternion.identity);
    31.  
    32.         StartCoroutine (SpawnPrefab(Random.Range (1f,2f)));
    33.             }
    34.  
    35.     void OnTriggerEnter2D(Collider2D col){
    36.         if (col.CompareTag("Player")){
    37.             hats.Damagesss (1);
    38.         }
    39.     }
    40.  
    41.  
    42.  
    43. }
    44.  
    45.  
    46.  
    47.  
    48.  
    49.  
    50.  
    51.  
    52.  
    53.  
    54.  
    55.  
    56.  
    57.  
    58.  
    59.  
    60.  
    61.  
    62.  
    63.  
    64.  
    65.  


    The spawner of the bomb doesnt instatnitate more than once when we attach a RigidBody2D to it. The rigidbody is to decrease health. The decreasing health works, but it only spawn once.
     
  2. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    Sure you can use the "poor mans loop", but you should really just add a while loop to your SpawnPrefab method like this:
    Code (csharp):
    1.  
    2. IENumerator SpawnPrefab()
    3. {
    4.     while(true)
    5.     {
    6.         Vector3 temp = transform.position;
    7.         temp.x = Random.Range(x1, x2);
    8.         Instantiate(prefab[Random.Range(0, 1)], temp, Quaternion.identity);
    9.         yield return new WaitForSeconds(Random.Range(1f, 2f));
    10.     }
    11. }

    As for the bomb disappearing, is your bomb prefab set to an actual prefab? Or a gameobject in the scene? To check if the spawn event is actually firing, add a print("Spawned bomb."); next to the instantiate call and make sure it's looping. If it is then you know the problem is in the prefab.
     
    Last edited: Jul 2, 2017
  3. froi0603

    froi0603

    Joined:
    Jun 12, 2017
    Posts:
    23


    Hi Thanks for taking the time to help us. Our bomb is a prefab. I tried putting a loop but its still instantiating it only once. The Spawned bomb only printed once.
     

    Attached Files:

  4. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    Hmm.. what object is the Bomb script attached to? Either the gameobject it's on is being deactivated/destroyed, or the coroutine is being stopped with StopCoroutine().
     
  5. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    sorry why don't you put in your update mode?