Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Flappy Bird Pipe Spawning Issue

Discussion in '2D' started by johnnysuriano, May 30, 2024.

  1. johnnysuriano

    johnnysuriano

    Joined:
    May 13, 2024
    Posts:
    14
    I'm following GMTK Unity for beginners Flappy Bird tutorial and I'm at the part where you get pipes to spawn at random y levels I put in all the code but there still only spawning at the same y level, any help would be appreciated, thanks

    PipeSpawnScript code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PipeSpawnScript : MonoBehaviour
    6. {
    7.     public GameObject pipe;
    8.     public float spawnRate = 2;
    9.     private float timer = 0;
    10.     public float heightOffset = 10;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         spawnPipe();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (timer < spawnRate)
    22.         {
    23.             timer = timer + Time.deltaTime;
    24.         }f
    25.         else
    26.         {
    27.             spawnPipe();
    28.             timer = 0;
    29.         }
    30.     }
    31.  
    32.     void spawnPipe()
    33.     {
    34.         float lowestPoint = transform.position.y - heightOffset;
    35.         float heightestPoint = transform.position.y + heightOffset;
    36.  
    37.         Instantiate(pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, heightestPoint), 0), transform.rotation);
    38.     }
    39. }
     
    Last edited: May 30, 2024
  2. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    137
    What is
    heightOffset
    set to in the Inspector?
     
  3. Proudani

    Proudani

    Joined:
    May 5, 2024
    Posts:
    8
    Firstly, make your code be like a code, for example:
    Code (CSharp):
    1. void Start()
    2. {
    3.  
    4. }
     
  4. Proudani

    Proudani

    Joined:
    May 5, 2024
    Posts:
    8
    Secondly, use coroutines to manage the spawn rate of the pipes more efficiently.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PipeSpawnScript : MonoBehaviour
    7. {
    8. public GameObject pipe;
    9. public float spawnRate = 2;
    10. public float heightOffset = 10;
    11. // Start is called before the first frame update
    12. void Start()
    13. {
    14.   StartCoroutine(managePipes());
    15. }
    16.  
    17. void spawnPipe()
    18. {
    19.   float lowestPoint = transform.position.y - heightOffset;
    20.   float heightestPoint = transform.position.y + heightOffset;
    21.  
    22.   Instantiate(pipe, new Vector3(transform.position.x,
    23.   Random.Range(lowestPoint, heightestPoint), 0),
    24.   transform.rotation);
    25. }
    26.  
    27. private IEnumerator managePipes()
    28. {
    29.   while (true)
    30.   {
    31.    spawnPipe();
    32.    yield return new WaitForSeconds(spawnRate);
    33.   }
    34. }
    35.  
     
  5. johnnysuriano

    johnnysuriano

    Joined:
    May 13, 2024
    Posts:
    14
    Not sure where I can find that, this is all thats under PipeSpawner in the inspector
     

    Attached Files:

  6. johnnysuriano

    johnnysuriano

    Joined:
    May 13, 2024
    Posts:
    14
    I'm following a tutorial and I'm worried if I completely change the code it might break something later on
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    @Proudani 's suggestion is great, but for right now, just hang tight and yes, definitely follow the tutorial correctly.


    Most likely you have typos.

    EDIT: you have an obvious typo on line 24. Go fix it.

    Read the console. Refer to the tutorial. Typing must be 100% perfect, including capitalization, punctuation, spelling, everything. If unsure, hit google. You're NEVER the first person to commit a typo and get an error.


    Once you fix it, yes, that's where the value will be for heightOffset. Make sure it is NOT zero. NOTE: it does not matter what you put in code, and here is why:



    Serialized / public fields in Unity are initialized as a cascade of possible values, each subsequent value (if present) overwriting the previous value:

    - what the class constructor makes (either default(T) or else field initializers, eg "what's in your code")

    - what may be saved with the prefab

    - what may be saved with the prefab override(s)/variant(s)

    - what may be saved in the scene and not applied to the prefab

    - what may be changed in the scene and not yet saved to disk

    - what may be changed in OnEnable(), Awake(), Start(), or even later

    Make sure you only initialize things at ONE of the above levels, or if necessary, at levels that you specifically understand in your use case. Otherwise errors will seem very mysterious.

    Here's the official discussion: https://blog.unity.com/technology/serialization-in-unity

    If you must initialize fields, then do so in the void
    Reset()
    method, which ONLY runs in the UnityEditor.

    Field initializers versus using Reset() function and Unity serialization:

    https://forum.unity.com/threads/sensitivity-in-my-mouselook-script.1061612/#post-6858908

    https://forum.unity.com/threads/crouch-speed-is-faster-than-movement-speed.1132054/#post-7274596

    To avoid complexity in your prefabs / scenes, I recommend NEVER using the
    FormerlySerializedAsAttribute
     
  8. Proudani

    Proudani

    Joined:
    May 5, 2024
    Posts:
    8
    I think I found the mistake, where is the script connected to? Is it a GameObject, an empty, or a pipe?

    Because on the line 22, If the x is not on the center of your game, the pipe might be spawning, but somewhere else.