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

Question Flappy prefab

Discussion in 'Scripting' started by TokoHagar, Jun 7, 2023.

  1. TokoHagar

    TokoHagar

    Joined:
    Apr 29, 2023
    Posts:
    9
    So I'm trying to make a flappy bird game. I have a pipe prefab with a pipe move script in it. To spawn the pipes, I do this:

    Code (CSharp):
    1. void spawnPipe()
    2.     {
    3.         float lowestPoint = transform.position.y - heightOfsset;
    4.         float highestPoint = transform.position.y + heightOfsset;
    5.  
    6.         Instantiate(pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
    7.     }
    Now, I want it so that when my bird dies, the pipes stop moving. So in my logic script, I have this:

    Code (CSharp):
    1. public void gameOver()
    2.     {
    3.         gameOverScreen.SetActive(true);
    4.         NEILSCREECH.Play();
    5.         pipeMove.enabled = false;
    6.         pipeSpawner.enabled = false;
    7.         Debug.Log("dead");
    8.     }
    "pipeMove.enabled = false;"

    It gets the job done, kinda. It does stop the pipe from moving, but only the first instance of the pipe, not the others. Is there a certain function I can add here so that it applies to ALL the instances?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,560
    Quickest and dirtiest is to do a FindObjectsOfType<T>() to find all the pipe movers, then iterate that collection and disable them all.

    The "clean" way is to manage those items in your own collection. Since this only happens at game over, I wouldn't bother. I would just use the quick-and-dirty way.
     
  3. TokoHagar

    TokoHagar

    Joined:
    Apr 29, 2023
    Posts:
    9
    So I tried doing this:
    Code (CSharp):
    1. public void gameOver()
    2.     {
    3.         gameOverScreen.SetActive(true);
    4.         NEILSCREECH.Play();
    5.         bobSpawner.enabled = false;
    6.         Debug.Log("dead");
    7.         PipeMove[] pipes = FindObjectsOfType<PipeMove>();
    8.         foreach (PipeMove pipe in pipes)
    9.         {
    10.             PipeMove.enabled = false;
    11.         }
    12.     }
    But it produces the same effect. I'm not even sure if I used the function correctly lol. Any advice?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,560
    This line should not compile:

    If it does compile then REMOVE the
    public static bool enabled;
    from your PipeMove class.

    Instead the above line should be:

    Code (csharp):
    1. pipe.enabled = false;
    I suggest you review the difference between
    static
    and instance identifiers in C#.

    Avoid any use of
    static
    until you understand why and how you would use that keyword.
     
    Last edited: Jun 8, 2023
    TokoHagar and DragonCoder like this.
  5. TokoHagar

    TokoHagar

    Joined:
    Apr 29, 2023
    Posts:
    9
    Holy crap IT WORKED! My thought process on putting "PipeMove.enabled = false;" in the script was to turn off the script that moved the pipe. I didn't know that I could turn off a game object as a whole!

    On second thought, I think it actually is the PipeMove getting disabled, just in the name of pipes. But anyways, thank you very much.