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. Dismiss Notice

Instantiate() Clones too many clones at once, I have a jam that is going to end. SO HELP PLEASE??

Discussion in 'Scripting' started by Kassem_m31, May 1, 2020.

  1. Kassem_m31

    Kassem_m31

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

    Kassem_m31

    Joined:
    Apr 17, 2020
    Posts:
    25
    please help me
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    First of all, there's some sort of forum etiquette.
    Next time, please describe the issue properly, do not just throw your code in.

    It's probably that you start too many coroutines, basically one each frame once the conditions evaluates to true. That'd be 60 hearts / second when you run stable 60 FPS.
    You want to prevent hearts from spawning either by changing the "timer" so that the condition evaluates to false, or by adding an additional counting mechanism that allows to skip the spawning routine when the max is reached.

    There are also other issues, but let's hope you can fix that problem in time.
     
  4. Kassem_m31

    Kassem_m31

    Joined:
    Apr 17, 2020
    Posts:
    25
    so what should i do exactly? sorry i'm a little new to unity
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    You haven't said what your desired behavior is. Nobody can help you if we don't know what it is you're trying to accomplish with your script.
     
    Joe-Censored and matkoniecz like this.
  6. matkoniecz

    matkoniecz

    Joined:
    Feb 23, 2020
    Posts:
    170
    Try to reduce number of coroutines controlling logic, it is overcomplicated to debug and can be accomplished without such methods.

    Also, wait function should be named Wait (capitalized), MovementTimer variable should be named movementTimer (not capitalized). Fix naming to standard one, use more descriptive names, remove coroutines.
     
  7. Kassem_m31

    Kassem_m31

    Joined:
    Apr 17, 2020
    Posts:
    25
    How can I reduce the number of Couroutines I need them both
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You reduce the number of coroutines by just not calling as many coroutines, but how best to do that depends on what you actually want, which for some reason you're unwilling to say.

    Is there some reason why you're keeping exactly what you're trying to accomplish a secret? Unfortunately my mind reading device is broken and the repair shop has been ordered closed due to the pandemic, so it would probably be helpful if you just stated what you want your code to actually do.
     
    Vryken, Suddoha and matkoniecz like this.
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    What? You're still using a mind reading device? That's so outdated!
     
    Vryken, Joe-Censored and matkoniecz like this.
  10. Kassem_m31

    Kassem_m31

    Joined:
    Apr 17, 2020
    Posts:
    25
    I want to make Hearts that get cloned every 3 seconds and move it to the left and the player tries to click it everything is working except cloning.
     
  11. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    My Jedi mind trick powers are woefully underdeveloped :(

    Then you create a coroutine containing a while loop. In the loop you instantiate your Heart object, and then yield return waitforseconds 3. You call this coroutine just one time, such as from Start, and the coroutine will continue to instantiate, wait 3, instantiate, wait 3, until you tell the coroutine to stop.
     
  12. Kassem_m31

    Kassem_m31

    Joined:
    Apr 17, 2020
    Posts:
    25
    Thank u so much!! you are amazing!
     
    Joe-Censored likes this.