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.

Instantiate Clones too many clones in onetime. HELP PLEASE?!

Discussion in 'Meta-forum Discussion' started by Kassem_m31, Apr 30, 2020.

  1. Kassem_m31

    Kassem_m31

    Joined:
    Apr 17, 2020
    Posts:
    25
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SpawningHearts : MonoBehaviour
    7. {
    8.     public RectTransform Heart;
    9.     public Button heart;
    10.     public float speed;
    11.     float timer = 1f;
    12.     float MovementTimer = 1f;
    13.     RectTransform HeartClone;
    14.     void Update()
    15.     {
    16.         if (timer == 1)
    17.         {
    18.             StartCoroutine(wait());
    19.             Clone();
    20.         }
    21.         if ( MovementTimer == 1)
    22.         {
    23.             StartCoroutine(Movement());
    24.             float random = Random.Range((-252), 256);
    25.             HeartClone.position = new Vector3(Heart.position.x, random, 0);
    26.             HeartClone.Translate(speed * (-1) * Time.deltaTime, 0, 0);
    27.         }
    28.     }
    29.     IEnumerator wait()
    30.     {
    31.         timer = 0;
    32.         yield return new WaitForSeconds(3);
    33.         timer = 1;
    34.     }
    35.     IEnumerator Movement()
    36.     {
    37.         MovementTimer = 0;
    38.         yield return new WaitForSeconds(0.5f);
    39.         MovementTimer = 1f;
    40.  
    41.     }
    42.     void Clone()
    43.     {
    44.        
    45.         HeartClone = Instantiate(Heart, Heart) as RectTransform;
    46.         Destroy(HeartClone.gameObject, 3f);
    47.     }
    48. }
    49.  
     
  2. Kassem_m31

    Kassem_m31

    Joined:
    Apr 17, 2020
    Posts:
    25
    can somebody please help me because I have Game Jam that ends tommorow and I really need help
     
  3. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,093
    Kassem_m31 likes this.