I am a new programmer and am making a flappy bird game as my first project. I have followed the tutorial I am watching to the point, but I still have a problem in one of my scripts. Everything works except that the last If statement is causing some problems. Without it, the code runs perfectly but the statement is important because it deletes the pipes when they move offscreen. I am using Unity with the c# language. Any help is appreciated.
I tried changing the statement but nothing happened. When i removed the statement it worked perfectly. The problem is that the pipes dont spawn at all with this statement in the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PipeMoveScript : MonoBehaviour
{
public float moveSpeed = 5;
public float deadZone = -45;
// 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);
}
}
}
From what it looks like the pipes probably move from right to left. This means that you shouldn't delete them when transform.position.x > deadZone but rather when it is less. The pipes seem to not spawn at all because the moment they spawn they also delete themselves as their x is obviously bigger than the deadZone that has been set. To the right of unity the x values increase and to the left they decrease so to fix it, in the if statement just change the condition to
transform.position.x < deadZone. I believe that should fix everything. Try it out.