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 artLightyear · Apr 08, 2014 at 09:27 AM · prefabrandomspawnprefabsprefab-instance

Problem: Random Instantiating more than one prefab?

Hello Everyone, i'm pretty new at coding, and I would like to spawn a prefab every X second (the time is set from the inspector). The script works fine, but I really can't understand why, once in a while, it instantiates two overlapping prefabs, and not just one. Any idea? :)

Thanks to everyone :)

 using UnityEngine;
 using System.Collections;
 
 public class Game_manager : MonoBehaviour {
     
 
     public Transform[] teleport;
     public GameObject[] prefab_tele;
     public float waitTime;
     bool spawning = false ; // are we spawning?
     
 
 
     IEnumerator SpawnPics() {
         spawning = true ; //going to spawn now
         int tele_num = Random.Range(0,teleport.Length);
         int prefab_num = Random.Range(0,prefab_tele.Length);
         Instantiate(prefab_tele[prefab_num], teleport[tele_num].position, teleport[tele_num].rotation );
             
         yield return new WaitForSeconds(waitTime);
         spawning = false ; //done spawning
     }
 
 
     void Update(){
         if(!spawning) 
             StartCoroutine(SpawnPics()) ;
     }
 }
     
     
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
Best Answer

Answer by Patel-Sagar · Apr 08, 2014 at 09:59 AM

well I tried your code. there seems no problem here. may be other script affecting your scene. you can try in another scene with only this script enabled.

If the problem still remain, you can try InvokeRepeating method of unity. for that just make one method with Instantiate Line, and then call it from start via Invoke repeat. It will look way nicer and less complex.

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 artLightyear · Apr 08, 2014 at 10:25 AM 0
Share

I tried to solve it with InvokeRepeating. It seems to work! thanks!

avatar image Patel-Sagar · Apr 09, 2014 at 05:44 AM 0
Share

well If tit satisfy your requirement, make it correct answer. so others can use this if they stuck at same place.

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

22 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

Related Questions

Spawn various enemies at random 1 Answer

Do I necessarily need to instantiate a prefab before putting it in List to conserve his variable data? 0 Answers

How to add components to all spawned prefabs ? 0 Answers

Zombie is working fine but it's prefab is creating issue in animation in unity,Zombie is working fine but it's prefab is creating issue in animation in unity Ask Question 0 Answers

Prefabs won't change? 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