Search Unity

So .. I have that code , and I want to Spawn something like a coin

Discussion in 'Scripting' started by iMihai9, Jun 15, 2018.

  1. iMihai9

    iMihai9

    Joined:
    Jun 4, 2018
    Posts:
    11
    I have that code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SpawnHazards : MonoBehaviour {
    7.     #region Variables
    8.     //Public
    9.  
    10.  
    11.     //Private
    12.     [SerializeField]
    13.     public float minX = 0.0f;
    14.     [SerializeField]
    15.     public float maxX = 0.0f;
    16.     [SerializeField]
    17.     private int minHazardToSpawn = 1;
    18.     [SerializeField]
    19.     private int maxHazardToSpawn = 6;
    20.     [SerializeField]
    21.     private GameObject[] hazards;    //Potential array of hazards
    22.     [SerializeField]
    23.     private float timeBetweenSpawns = 0.0f;
    24.     public bool canSpawn = false;
    25.     private int amountOfHazardToSpawn = 0;
    26.     private int hazardToSpawn = 0;
    27.  
    28.     private UIFunctions uiFunctions;
    29.     #endregion
    30.  
    31.     #region UnityFunctions
    32.     void Start()
    33.     {
    34.         uiFunctions = GameObject.FindGameObjectWithTag("GameManager").GetComponent<UIFunctions>();
    35.         canSpawn = true;
    36.     }
    37.  
    38.  
    39.     void Update () {
    40.         if(canSpawn == true && uiFunctions.gameStarted == true)
    41.         {
    42.             StartCoroutine("GenerateHazard");
    43.            
    44.         }
    45.  
    46.     }
    47.     #endregion
    48.  
    49.     private IEnumerator GenerateHazard()
    50.     {
    51.        
    52.         canSpawn = false;
    53.         timeBetweenSpawns = Random.Range(0.5f, 2.0f);
    54.         amountOfHazardToSpawn = Random.Range(minHazardToSpawn, maxHazardToSpawn);
    55.         for(int i = 0; i < amountOfHazardToSpawn; i++)
    56.         {
    57.             Vector3 spawnPos = new Vector3(Random.Range(minX, maxX), Random.Range(8.5f, 16f), -4.079776f);   //Generate s spawnPosition
    58.             Instantiate(hazards[hazardToSpawn], spawnPos, Quaternion.identity);
    59.         }
    60.         yield return new WaitForSeconds(timeBetweenSpawns);
    61.         canSpawn = true;
    62.     }
    63. }
    64.  
    65.  
    I actually want to add a new spawn object and when that object hit my player , i want to add a coin :))
    I tried different method but don't work ..
    That's my collision script :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6. using UnityEngine.Audio;
    7.  
    8. public class HazardCollisionFunction : MonoBehaviour {
    9.  
    10.     #region Variables
    11.  
    12.     //Public
    13.  
    14.  
    15.     //Private
    16.     private UIFunctions uiFunctions;
    17.     public ParticleSystem hazardDustParticles;
    18.  
    19.  
    20.  
    21.     #endregion
    22.  
    23.     #region UnityFunctions
    24.     void Start()
    25.     {
    26.         uiFunctions = GameObject.FindGameObjectWithTag("GameManager").GetComponent<UIFunctions>();
    27.  
    28.     }
    29.  
    30.     void Update()
    31.     {
    32.  
    33.  
    34.     }
    35.     #endregion
    36.  
    37.     private void OnCollisionEnter(Collision collision)
    38.     {
    39.         if(collision.gameObject.tag == "Platform")
    40.         {
    41.             Destroy(Instantiate(hazardDustParticles.gameObject, transform.position, Quaternion.identity), hazardDustParticles.startLifetime);
    42.  
    43.             Die();
    44.            
    45.  
    46.         }
    47.         if (collision.gameObject.tag == "Player")
    48.         {
    49.             Die();
    50.             uiFunctions.GameEnded();
    51.            
    52.         }
    53.  
    54.     }
    55.  
    56.     public void Die()
    57.     {
    58.         Destroy(gameObject);
    59.     }
    60. }
    61.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    "dont work" how?
     
  3. iMihai9

    iMihai9

    Joined:
    Jun 4, 2018
    Posts:
    11
    I create that:
    Code (CSharp):
    1.    private IEnumerator GenerateSphereHazard()
    2.     {
    3.  
    4.         canSpawn = false;
    5.         timeBetweenSpawns = Random.Range(0.5f, 2.0f);
    6.         amountOfHazardToSpawn = Random.Range(minHazardToSpawn, maxHazardToSpawn);
    7.         for (int i = 0; i < amountOfHazardToSpawn; i++)
    8.         {
    9.             Vector3 spawnPos = new Vector3(Random.Range(minX, maxX), Random.Range(8.5f, 16f), -4.079776f);   //Generate s spawnPosition
    10.             Instantiate(sphereHazard, spawnPos, Quaternion.identity);
    11.         }
    12.         yield return new WaitForSeconds(timeBetweenSpawns);
    13.         canSpawn = true;
    14.     }
    and that
    Code (CSharp):
    1.     void Update () {
    2.         if(canSpawn == true && uiFunctions.gameStarted == true)
    3.         {
    4.             StartCoroutine("GenerateHazard");
    5.             StartCoroutine("GenerateSphereHazard");
    6.            
    7.         }
    8.  
    9.     }