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 /
  • Help Room /
avatar image
0
Question by Whayman · May 12, 2017 at 12:05 PM · prefabrandomspawnspawningrandom.range

How to spawn a prefab every 5 second randomly between a set range?

I'm struggling to spawn a prefab in without it being a gameobject, and I want it one to spawn in every 5 seconds between a set range? any help would be appreciated!

public class obstacle : MonoBehaviour {

 private Rigidbody2D rb2d;
 private float obstacleSpawnCounter;
 public Vector3 spawnPosition;
 public GameObject spike;

 // Use this for initialization
 void Start () {

     rb2d = GetComponent<Rigidbody2D>();
     rb2d.isKinematic = true;
     GetComponent<SpriteRenderer>().sortingOrder = 4;

 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetMouseButtonDown(0))
     {
         if (rb2d.isKinematic)
             rb2d.isKinematic = false;
     }
     obstacleSpawnCounter += Time.deltaTime;
     if (obstacleSpawnCounter > 5)
     {
         SpawnObstacle();
         obstacleSpawnCounter -= 5;
     }
     this.transform.Translate(0, 0.06f, 0);
 }

 public void SpawnObstacle()
 {
     Instantiate(spike, spawnPosition, Quaternion.identity);
 }

}

Comment
Add comment · Show 3
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 Matthewj866 · May 12, 2017 at 02:56 PM 0
Share

Hi there, could you elaborate on your question a little more? I'm not entirely sure I understand what you're after.

avatar image Whayman Matthewj866 · May 12, 2017 at 03:33 PM 0
Share

Hi, I basically want to spawn a prefab onto the scene without it being attached to an empty gameobject and to spawn randomly between a set range every 5 seconds. At the moment it will only spawn when I make an empty gameobject and attach the prefab and script but it glitches every time it spawns and spawns like 20, any ideas??

avatar image Matthewj866 Whayman · May 12, 2017 at 03:49 PM 0
Share

Ok, you need to give it the spike prefab not an empty GameObject I'm still not entirely sure what the random value is for but $$anonymous$$ath.Random() exists so look into that.

When resetting a float responsible for handling deltaTime it's best to reset using 0 rather than removing the value you think it has, as doing it via the latter can create irregular behaviour.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how to Spawn prefab infront off player and destroy it when hr passed it 0 Answers

Instantiating prefab causes existing instances to change values 1 Answer

How do you spawn a gameobject based on last object in array and limit that within a range? 1 Answer

Random Object Spawn 0 Answers

Random spawn on screen 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