Question by
unity_13579aarok · Feb 15 at 08:52 AM ·
scripting problemerrorcrashing
Spam Spawns
Hi. I'm making a version of Tetris where each piece consists of 6 blocks I'm trying to spawn the pieces in but this occurs
My code for spawning is down here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.Random;
public class spritespawner : MonoBehaviour
{
public GameObject[] parts;
public GameObject have;
public GameObject had;
public bool oncespawned = false;
public int num;
// Start is called before the first frame update
void Start()
{
num = Random.Range(0,15);
have = parts[num];
had = have;
}
// Update is called once per frame
void Update()
{
if (oncespawned == false)
{
print(oncespawned);
num = Random.Range(0,14);
oncespawned = true;
print(oncespawned);
had = have;
have = parts[num];
had.GetComponent<Otherpieces>().changepiece(have);
}
}
public void summon()
{
have = Instantiate (parts[num], new Vector2(0, 2.5f), Quaternion.identity);
changeit();
print(oncespawned);
had = have;
// print(have);
}
public void begin()
{
summon();
changeit();
had.GetComponent<Otherpieces>().startup(have);
had.GetComponent<Otherpieces>().changepiece(have);
}
public void changeit()
{
oncespawned = false;
}
}
What's supposed to happen is that a piece spawns. The spawn is recorded and the spawner tells the piece to start falling. Once the piece reaches the bottom, the piece tells the spawner to spawn another piece. The problem, of course, is that it spawns way too many.
How can this be fixed?
Comment