So I’ve recently started learning C# through school and thought game development would be a fun side hobby. I started with a simple flappy bird tutorial on Youtube made by “Game Maker’s Toolkit” and when i was done i wanted to implement a feature of my own. In his tutorial, when you die and sit in the game over screen, the pipes keep spawning and moving in the background so i wanted to try to get the pipes to stop when you die since the reasons the pipes are moving is to simulate the bird moving. After 2 days of head scratching, reading forums and different unity documentation, coding and then some more head scratching i cant seem to figure out how to do this.
I have a script on a empty game object that spawns prefabs of the pipes and then a script inside the prefab that makes them move. I also have a script on another empty game object that handles a game over screen when u die, what i want to do is change the move speed of the pipes to “0” from the game over script and also change so it doesn’t spawn more pipes. I’ve succeeded in stopping more pipes from spawning but the ones already spawned keep moving since it doesn’t seem like i can change a prefabs scripts variables from another script?
I’ll put all the scripts i have down below and maybe some of u can point me in the right direction
Bird script
using UnityEngine;
public class BirdScript : MonoBehaviour
{
public Rigidbody2D myRigidbody;
public float flapStrength;
public LogicScript logic;
public bool birdIsAlive = true;
// Start is called before the first frame update
void Start()
{
logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) == true && birdIsAlive == true)
{
myRigidbody.velocity = Vector2.up * flapStrength;
}
if (transform.position.y < -60 || transform.position.y > 60)
{
logic.gameOver();
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
logic.gameOver();
birdIsAlive = false;
}
}
PipeMoveScript
using UnityEditor;
using UnityEngine;
public class PipeMoveScript : MonoBehaviour
{
public float moveSpeed = 5;
public float deadZone = -45;
public LogicScript logic;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = transform.position + (Vector3.left * moveSpeed) * Time.deltaTime;
if (transform.position.x < deadZone)
{
Destroy(gameObject);
}
}
}
PipeSpawnerScript
using UnityEngine;
public class PipeSpawnerScript : MonoBehaviour
{
public GameObject pipe;
public float SpawnRate = 3;
private float timer = 0;
public float pipeOffset = 10;
public bool spawning = true;
// Start is called before the first frame update
void Start()
{
SpawnPipe();
}
// Update is called once per frame
void Update()
{
if (timer < SpawnRate || spawning == false)
{
timer += Time.deltaTime;
}
else
{
SpawnPipe();
timer = 0;
}
}
void SpawnPipe()
{
float lowestPoint = transform.position.y - pipeOffset;
float highestPoint = transform.position.y + pipeOffset;
Instantiate(pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
}
}
Logic Script
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LogicScript : MonoBehaviour
{
public int playerScore;
public Text scoreText;
public GameObject gameOverScreen;
public PipeSpawnerScript spawn;
public PipeMoveScript move;
void Start()
{
spawn = GameObject.FindGameObjectWithTag("Stop Pipe").GetComponent<PipeSpawnerScript>();
move = GameObject.FindGameObjectWithTag("Stop Pipe").GetComponent<PipeMoveScript>();
}
[ContextMenu("Increase Score")]
public void addScore()
{
playerScore = playerScore + 1;
scoreText.text = playerScore.ToString();
}
[ContextMenu("Decrease Score")]
public void removeScore()
{
playerScore = playerScore - 1;
scoreText.text = playerScore.ToString();
}
public void restartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void gameOver()
{
gameOverScreen.SetActive(true);
spawn.spawning = false;
move.moveSpeed = 0;
}
PipeMiddleScript
using UnityEngine;
public class PipeMiddleScript : MonoBehaviour
{
public LogicScript logic;
// Start is called before the first frame update
void Start()
{
logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.layer == 3)
{
logic.addScore();
}
}
}