Question by
MochaMilk · Apr 10, 2017 at 10:37 PM ·
destroyscorespawningscore systemspawning problems
Spawn Object on Set Score
I am making a flappy bird clone for fun but I wanted to see if I could add objects to it for spawning. When I run this code, it tells me the GameObject I am referencing has been destroyed. I don't know why it keeps saying that.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour {
public GameObject birdPrefab;
public GameObject birdPrefab2;
public Transform birdSpawn;
public float minTime;
public float maxTime;
public float timer;
public PlayerController PC;
public Text score;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (timer <= 0 && PC.isStart == true)
{
BirdSpawner();
BirdSpawner2();
}
timer -= Time.deltaTime; // timer = timer - time.deltaTime;
scoreFunction();
}
void BirdSpawner()
{
Instantiate(birdPrefab, birdSpawn.position, birdSpawn.rotation);
timer = Random.Range(minTime, maxTime);
}
void BirdSpawner2()
{
Instantiate(birdPrefab2, birdSpawn.position, birdSpawn.rotation);
timer = Random.Range(minTime, maxTime);
}
void scoreFunction()
{
score.text = PC.playerScore.ToString();
}
public void ReplayFunction()
{
Time.timeScale = 1;
SceneManager.LoadScene(1);
}
public void MainMenuButton()
{
Time.timeScale = 1;
SceneManager.LoadScene(0);
}
}
Comment