Hi there I'm new to unity and C# and i want to make my first Android game and I'm having a problem when my player collides with the cratepipe in the game the spawning crate wont stop i need help with this i want to stop the spawning when the gameobject "player" hits the crates and stop. my player move by tilting
I'm going to leave all the scripts I'm using so far please help me what should i do....?
The Countdown Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CountdownText : MonoBehaviour
{
public delegate void CountdownFinished();
public static event CountdownFinished OnCountdownFinished;
Text countdown;
void OnEnable()
{
countdown = GetComponent<Text>();
countdown.text = "3";
StartCoroutine("Countdown");
}
IEnumerator Countdown()
{
int count = 3;
for (int i = 0; i < count; i++)
{
countdown.text = (count - i).ToString();
yield return new WaitForSeconds(1);
}
OnCountdownFinished();
}
}
Crate Script
sing UnityEngine;
public class Crate : MonoBehaviour {
public bool hit = false;
void OnTriggerEntre2D(Collider2D collider)
{
if (collider.tag =="Player")
{
hit = true;
}
}
}
CrateSpawn Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CrateSpawn : MonoBehaviour {
List<Cratework> cratePool;
int index = -1;
public GameObject cratePrefab;
public float spawnTime = 4f;
public float speed = 35;
public int poolSize = 5;
public bool isRunning = true;
[HideInInspector]
public bool hit = false;
void Start()
{
cratePool = new List<Cratework>();
for (int i = 0; i < poolSize; i++)
{
var crate = (GameObject)Instantiate(cratePrefab, new Vector3(0, 8, 0), Quaternion.identity);
cratePool.Add(crate.GetComponent<Cratework>());
}
StartCoroutine(SpawnCrate());
}
void Update()
{
for (int i = 0; i <= poolSize; i++)
{
var crate = cratePool;
crate.transform.position += new Vector3(0, speed * Time.deltaTime, 0);
if (crate.hit)
{
hit = true;
StopAllCoroutines(SpawnCrate());
}
}
}
IEnumerator SpawnCrate()
{
while (isRunning)
{
index++;
index %= poolSize;
var crate = cratePool[index];
float x = Random.Range(-2, 2);
crate.transform.position = new Vector3(x, -6, 0);
yield return new WaitForSeconds(spawnTime);
}
}
}
CrateWork Script
using UnityEngine;
public class Cratework : MonoBehaviour {
public Crate left;
public Crate right;
public bool hit = false;
void Update ()
{
if (left.hit || right.hit)
{
hit = true;
}
}
}
GameController Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour {
public CrateSpawn crateSpawn;
void ChangeSpeed(float speed)
{
crateSpawn.speed = speed;
}
void Update()
{
if (crateSpawn.hit)
{
ChangeSpeed(0);
}
}
}
GameManager Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
public delegate void GameDelegate();
public static event GameDelegate OnGameStarted;
public static event GameDelegate OnGameOverConfirmed;
public static GameManager Instance;
public GameObject startPage;
public GameObject gameOverPage;
public GameObject countdownPage;
public Text scoreText;
enum PageState
{
None,
Start,
GameOver,
Countdown
}
int score = 0;
bool gameover = true;
public bool GameOver { get { return gameover; } }
void Awake()
{
Instance = this;
}
void OnEnable()
{
CountdownText.OnCountdownFinished += OnCountdownFinished;
MoveController.OnPlayerDied += OnPlayerDied;
MoveController.OnPlayerScored += OnPlayerScored;
}
void OnDisable()
{
CountdownText.OnCountdownFinished -= OnCountdownFinished;
MoveController.OnPlayerDied -= OnPlayerDied;
MoveController.OnPlayerScored -= OnPlayerScored;
}
void OnCountdownFinished()
{
SetPageState(PageState.None);
OnGameStarted();
score = 0;
gameover = false;
}
void OnPlayerDied()
{
gameover = true;
int savedScore = PlayerPrefs.GetInt("HighScore");
if (score > savedScore)
{
PlayerPrefs.SetInt("HighScore", score);
}
SetPageState(PageState.GameOver);
}
void OnPlayerScored()
{
score++;
scoreText.text = score.ToString();
}
void SetPageState(PageState state)
{
switch (state)
{
case PageState.None:
startPage.SetActive(false);
gameOverPage.SetActive(false);
countdownPage.SetActive(false);
break;
case PageState.Start:
startPage.SetActive(true);
gameOverPage.SetActive(false);
countdownPage.SetActive(false);
break;
case PageState.GameOver:
startPage.SetActive(false);
gameOverPage.SetActive(true);
countdownPage.SetActive(false);
break;
case PageState.Countdown:
startPage.SetActive(false);
gameOverPage.SetActive(false);
countdownPage.SetActive(true);
break;
}
}
public void ConfirmGameOver()
{
OnGameOverConfirmed();//event
scoreText.text = "0";
SetPageState(PageState.Start);
}
public void StartGame()
{
SetPageState(PageState.Countdown);
}
}
HighScore Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HighscoreText : MonoBehaviour
{
Text highscore;
void OnEnable()
{
highscore = GetComponent<Text>();
highscore.text = ": " + PlayerPrefs.GetInt("HighScore").ToString();
}
}
MoveController Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveController : MonoBehaviour {
public delegate void PlayerDelegate();
public static event PlayerDelegate OnPlayerDied;
public static event PlayerDelegate OnPlayerScored;
public float speed = 0.25f;
public Vector3 startPos;
Rigidbody2D rigidbody;
GameManager game;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
game = GameManager.Instance;
}
void OnEnable()
{
GameManager.OnGameStarted += OnGameStarted;
GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
}
void OnDisable()
{
GameManager.OnGameStarted -= OnGameStarted;
GameManager.OnGameOverConfirmed -= OnGameOverConfirmed;
}
void OnGameStarted()
{
rigidbody.velocity = Vector3.zero;
rigidbody.simulated = true;
}
void OnGameOverConfirmed()
{
transform.localPosition = startPos;
}
void Update()
{
transform.Translate(Input.acceleration.x * speed, 0, 0);
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "ScoreZone")
{
OnPlayerScored();
//play a sound
//get a point
}
if (col.gameObject.tag == "DeadZone")
{
OnPlayerDied();
rigidbody.simulated = false;
}
}
}