- Home /
Starting script after tap/click
I've got an EnemySpawner object with a script attached. The script simply spawns the enemies in my scene. As of right now (before trying to implement the tap to start) the script runs and the enemies spawn immediately after clicking the play button. What I want to do is implement a 'Tap to start' in my scene where the enemies start spawning only after the player taps the screen. I figured I must somehow use the Start function but I'm not entirely sure.
My EnemySpawner script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawnerScriptt : MonoBehaviour
{
public GameObject enemy;
public GameObject enemy2;
public GameObject enemy3;
float randX;
Vector2 whereToSpawn;
public float spawnRate = 2f;
float nextSpawn = 0.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) == true)
{
bool started = true;
while (started == true)
{
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range(-2.4f, 2.4f);
GameObject[] enemyArray = { enemy, enemy2, enemy3 };
int randEnemy = UnityEngine.Random.Range(0, enemyArray.Length);
whereToSpawn = new Vector2(randX, transform.position.y);
Instantiate(enemyArray[randEnemy], whereToSpawn, Quaternion.identity);
}
}
}
}
}
This doesn't work. I need just one initial click to start the script and if this matters I want to be able to implement a replay button later on which starts the game again from an initial click to start.
Answer by PhotonPotatoDev · Apr 27, 2021 at 11:37 PM
Hey @bobmcdonal, try moving the while loop to be outside the input if statement. Or just delete the while loop (not the stuff inside) and put this line of code above the left over stuff inside the while loop:
if (started != true) return;
I've tried this but it's only spawning 1 enemy per click. It should be spawning continuously after 1 initial click.
Didn't see your edit, let me try and then get back to you. Yup, Same issue, 1 spawn per click
@bobmcdonal I see the bug now. You need to declare the boolean at the top of the script! (and set it to default false) Here is your code, just edited: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemySpawnerScriptt : MonoBehaviour {
public GameObject enemy;
public GameObject enemy2;
public GameObject enemy3;
float randX;
Vector2 whereToSpawn;
public float spawnRate = 2f;
float nextSpawn = 0.0f;
bool started = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) == true)
{
started = true;
}
if (started != true) return;
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range(-2.4f, 2.4f);
GameObject[] enemyArray = { enemy, enemy2, enemy3 };
int randEnemy = UnityEngine.Random.Range(0, enemyArray.Length);
whereToSpawn = new Vector2(randX, transform.position.y);
Instantiate(enemyArray[randEnemy], whereToSpawn, Quaternion.identity);
}
}
}
On a side note you may wanna use if (Time.time > nextSpawn && started)
as opposed to stopping the Update()
midway with if (started != true) return;
. If it's forgotten about it may cause issues if anything gets placed beyond later on.