Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ajoel-37 · Nov 17, 2018 at 03:12 PM · spawnif statementspawning problemsinvokerepeatingdestroygameobject

,Can i use InvokeRepeating to spawn just 1 GameObject at a time?

I'm trying to spawn collectible coins randomly within my game, and i have managed to get this working by doing the following:

 public GameObject coin; // prefab to be spawned
 public Transform[] spawnPosition;  // creates ref which is used in line 18
 public float spawnWait = 3.0f; // time between each spawn

 void Start()
 {
     InvokeRepeating("Spawn", spawnWait, spawnWait); 
 }
 
 void Spawn()
 {       
         int spawnIndex = Random.Range(0, spawnPosition.Length);// create int between no. of spawnpositions
         Instantiate(coin, spawnPosition[spawnIndex].position, spawnPosition[spawnIndex].rotation);
 }


However, this does not fit the purpose of my game.
I only want to have 1 coin in play at a time, whereas this code will spawn a new coin every 3 seconds. This code will also sometimes spawn coins on top of one another when left to run for too long.
Is it possible to recall this function only after the previously cloned GameObject has been destroyed?

I have tried adding an if statement of (!=coin) within the spawn() function, but this does not work so I'm not even sure whether this is even the correct logic!

As you can probably tell, I'm a complete beginner and so any advise would be awesome!

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 Captain_Pineapple · Nov 17, 2018 at 05:00 PM

Hey there,

the probably easiest solution would be pack the span function in your coin. When you pick it up use 'Invoke()' instead of invokerepeating and spawn one after 3 seconds.

An even better solution would be to have your current spawn function be callable from your coin. On picking it up you notify your "Gamemanager" to disable your coin and reable it after 3 seconds. Instatiating is performance hungry. En/Disabling is not

If it's not really clear what i mean let me know and i'll try to specify.

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

97 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

Related Questions

How to Instantiate prefabs with function Update? 1 Answer

How to stop instantiate spawn ? 1 Answer

How do I add a timer /wait time & limiter for Spawning? 1 Answer

How do I make a game object spawn and then stay and move up when mouse is cilcked? 1 Answer

Multiple Spawn Points for fps character 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