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 Mjhurtado1 · Feb 11, 2015 at 01:16 PM · gameobjectrandomtimespawner

Spawning an object at a random time | C#

I'm creating a C# script that counts to a random number, once its done counting it will spawn an object.

The problem is, sometimes the spawning is off and the objects get spawned on top of one another.

The code: using UnityEngine; using System.Collections;

 public class RandomTimeSpawner : MonoBehaviour {
 
     //Spawn this object
     public GameObject spawnObject;
 
     public float maxTime = 5;
     public float minTime = 2;
 
     //current time
     private float time;
 
     //The time to spawn the object
     private float spawnTime;
 
     void Start(){
         SetRandomTime();
         time = minTime;
     }
 
     void FixedUpdate(){
 
         //Counts up
         time += Time.deltaTime;
 
         //Check if its the right time to spawn the object
         if(time >= spawnTime){
             SpawnObject();
             SetRandomTime();
         }
 
     }
 
 
     //Spawns the object and resets the time
     void SpawnObject(){
         time = minTime;
         Instantiate (spawnObject, transform.position, spawnObject.transform.rotation);
     }
 
     //Sets the random time between minTime and maxTime
     void SetRandomTime(){
         spawnTime = Random.Range(minTime, maxTime);
     }
 
 }
 


Any help would be greatly appreciated! :D

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 Bonfire-Boy · Feb 11, 2015 at 01:20 PM 1
Share

Don't you want to be resetting time to 0 in SpawnObject, rather than to $$anonymous$$Time? As things stand you're spawning at an interval that's randomised between 0 and (maxTime-$$anonymous$$Time), rather than between $$anonymous$$Time and maxTime which I'm assu$$anonymous$$g is what's intended.

avatar image chariot · Feb 11, 2015 at 01:21 PM 1
Share

Why "time = $$anonymous$$Time"? I guess u need "time = 0" at SpawnObject() function. If your time always equals $$anonymous$$Time your spawner can get "spawnTime" with same number, and thats why you have multiple spawns.

avatar image Mjhurtado1 · Feb 11, 2015 at 07:18 PM 0
Share

Thank you very much, that didn't even cross my $$anonymous$$d! :D

2 Replies

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

Answer by jenci1990 · Feb 11, 2015 at 03:25 PM

 void SpawnObject(){
     time = 0; //Change this line
     Instantiate (spawnObject, transform.position, spawnObject.transform.rotation);
 }
Comment
Add comment · Show 4 · 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 Mjhurtado1 · Feb 11, 2015 at 07:17 PM 0
Share

Thanks for the answer!

avatar image Zeesy · May 25, 2017 at 10:09 PM 0
Share

I am using this (great) script, but am having a problem. I can see the clones as they are generated in the Hierarchy, but certain prefabs are not visible in the game. Any idea what is causing this? I have tried to space out my empty objects as much as possible.

avatar image Bonfire-Boy Zeesy · May 25, 2017 at 11:14 PM 0
Share

@Zeesy There are loads of possibilities. I'd probably start by deactivating them one at a time (in the inspector) to identify which ones aren't visible. Then focus on those: are they within sight of the camera? (you can see their positions in the inspector and also look at the scene view) If you're not sure you can just use the inspector to move them to the same place as one of the visible ones, are they now visible? If not, are they in the right layer? Check the renderer/material, and so on. Or is it just that some have been spawned at the same place as each other?

By the way, I am assu$$anonymous$$g that you meant to say "...but certain clones are not visible in the game".

avatar image Zeesy Bonfire-Boy · May 29, 2017 at 05:36 PM 0
Share

Thanks, @Bonfire-Boy. I moved the empty objects around, and tightened up the prefabs' colliders, and everything seems to be working fine now. Next challenge is to have them spawn at random locations...

avatar image
1

Answer by Dani-Desa · Feb 03, 2018 at 11:40 PM

This is my version:

 using UnityEngine;
 
 using System.Collections.Generic;
 
 
 public class EnemySpawner : MonoBehaviour
 {
     // The enemy prefab to be spawned.
     public GameObject enemy;
 
     // An array of the spawn points this enemy can spawn from.
     private Transform[] spawnPoints;
 
     public float maxTime = 50;
 
     public float minTime = 10;
 
     //current time
     private float time;
  
     //The time to spawn the object
     private float spawnTime;
 
 
     void Awake ()
     {
 
         List<Transform> spawningPointsAsList = new List<Transform> ();
 
         // Get All the children of the object this script is assigned to (EnemyManager) and consider them as spawining points
         foreach (Transform child in transform) {
             spawningPointsAsList.Add (child);
         }
 
         spawnPoints = spawningPointsAsList.ToArray ();
     }
 
 
     void Start ()
     {
         SetRandomTime ();
         time = 0;
     }
 
     //Sets the random time between minTime and maxTime
     void SetRandomTime ()
     {
         spawnTime = Random.Range (minTime, maxTime);
         Debug.Log ("Next object spawn in " + spawnTime + " seconds.");
     }
 
     void FixedUpdate ()
     {
         //Counts up
         time += Time.deltaTime;
         //Check if its the right time to spawn the object
         if (time >= spawnTime) {
             Debug.Log ("Time to spawn: " + enemy.name);
             Spawn ();
             SetRandomTime ();
             time = 0;
         }
     }
 
 
     void Spawn ()
     {
         // Find a random index between zero and one less than the number of spawn points.
         int spawnPointIndex = Random.Range (0, spawnPoints.Length);
         // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
         Instantiate (enemy, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
     }
 }
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

8 People are following this question.

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

Related Questions

c# Enemy's spawning over time, problem 2 Answers

C# 2d Instantiate object randomly around gameobject 1 Answer

Randomize Spawn Times 0 Answers

Trying to delay movement of a GameObject 1 Answer

How to change the colour of a light every few seconds. 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