Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bobmcdonal · Apr 27, 2021 at 11:35 PM · 2dinputspawninginput.touch

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bobmcdonal · Apr 27, 2021 at 11:46 PM 0
Share

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

avatar image PhotonPotatoDev · Apr 28, 2021 at 06:47 PM 0
Share

@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);
      }
  }

}

avatar image HellsHand PhotonPotatoDev · Apr 28, 2021 at 08:11 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

304 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a flawless rhythm-like mechanism in mobile 2D? 0 Answers

how can i touch the fast objects ? 0 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Spawning objects from top of screen, Javascript 0 Answers

How to jump in 2D? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges