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 sebrobitaille · Apr 08, 2014 at 04:35 AM · delay

Delaying Spawning a Object

I a have script that spawns a object on the mouse click. But since the update function is called every frame the objects are spawned very fast. Is there away to delay the spawning a little bit so the object don't spawn as fast? Here is the script:


 using UnityEngine;
 using System.Collections;
 public class SpawnApple : MonoBehaviour {
     private Vector3 SpawnPoint;
     public Transform ApplePrefab;
     private bool canSpawn;
     // Use this for initialization
     void Start () {
         SpawnPoint = new Vector3(0,0,0);
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetMouseButton(0))
         {
             SpawnPoint =  camera.ScreenToWorldPoint(Input.mousePosition);
             SpawnPoint.z = 0;
             Instantiate(ApplePrefab,SpawnPoint, gameObject.transform.rotation);
         }
     
     }
 }


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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Darwin Mecharov · Apr 08, 2014 at 05:03 AM

Why not try to do GetMouseButtonDown/Up instead so that it only spawns the object once per click. Or, you could try using InvokeRepeating like this InvokeRepeating("SpawnObject", givenSeconds) to spawn an object every given amount of seconds even while mouse is held. Didn't test it yet though.

Comment
Add comment · Show 2 · 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 sebrobitaille · Apr 08, 2014 at 02:41 PM 0
Share

Thanks, that works great. I don't know why I didn't think of that.

avatar image Darwin Mecharov · Apr 08, 2014 at 03:12 PM 0
Share

Glad to help :)

avatar image
0

Answer by supernat · Apr 08, 2014 at 05:05 AM

See the answer I just posted here: http://answers.unity3d.com/questions/682547/c-custom-slower-update-function.html

In summary, yes, you can put a timer in to run the code at a periodic interval through a CoRoutine or by creating a timeout that is tested every frame.

If you don't want to spawn except when the mouse first goes down, there's two ways to do that. You can add a bool variable mouseDown to your script and use it for edge detection:

 bool mouseDown;

 void Update () {
    if(Input.GetMouseButton(0) && !mouseDown)
    {
      SpawnPoint =  camera.ScreenToWorldPoint(Input.mousePosition);
      SpawnPoint.z = 0;
      Instantiate(ApplePrefab,SpawnPoint, gameObject.transform.rotation);
    }
    mouseDown = Input.GetMouseButton(0);
 }

Or, if you only want to do this when the mouse is clicked on a collider attached to this script, you can do this:

 void OnMouseDown() {
     // Instantiate the object
 }
Comment
Add comment · 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

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

23 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

Related Questions

Make delay for spawn 3 Answers

Unity Analytics Bug/Delay 4 Answers

keybind problem 0 Answers

Putting Cooldown for flipping and moving the cube 1 Answer

Logical Error In Spawn Counter (Javascript) 2 Answers


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