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 isaacbonanni86 · Feb 22, 2020 at 05:07 AM · timeobjectsspawningwith

i want tospawn objects

i want to spawn n egg like from snake game, but i want to spawn like, every 5 seconds spawn one, and after 5 seconds destroy it, and if destroys the player loses one life, if he loses 3 he losts, how can i do the egg spawning?i tried with instantiate but it keeps spawning over and over, the code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class eggs : MonoBehaviour { public Vector3 center; public Vector3 size; public GameObject Eggprefab; private float timeElapsed;

[SerializeField] private float delayEggs = 5f; [SerializeField] private float startDelay = 0f; [SerializeField] private float timeForSpawnEggs = 1f;

void Start(){

}

void Update(){ timeElapsed += Time.deltaTime; if (timeElapsed >= 4.99999 || timeElapsed <= 5.00001){ SpawnEggs ();}

}

void SpawnEggs(){ Vector2 pos = center = new Vector2(Random.Range(-size.x / 2, size.x / 2), Random.Range(-size.y /2, size.y / 2));

Instantiate(Eggprefab, pos, Quaternion.identity);

}

void OnDrawGizmosSelected() {Gizmos.color = new Color(1,0,0,0.5f); Gizmos.DrawCube(center, size);} }

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 Major_Lag · Feb 22, 2020 at 06:06 AM

You want to spawn every 5 seconds but in your update you're checking if time is greater than 4.9 seconds OR less than 5 which is a tautology. if you want to spawn in a interval it would be best to create a float defaulted to the spawn interval and then in update decrement it by deltatime and check if it reaches or goes below 0. If it does spawn your egg and reset your float back to your interval and repeat.

would probably look like:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class eggs : MonoBehaviour
 {
     public Vector3 center;
     public Vector3 size;
     public GameObject Eggprefab;
 
     public float spawnPeriod = 5;
     float timeElapsed;
 
     [SerializeField] float delayEggs = 5f;
     [SerializeField] float startDelay = 0f;
     [SerializeField] float timeForSpawnEggs = 1f;
 
     void Start()
     {
         timeElapsed = spawnPeriod;
     }
 
     void Update()
     {
 
         timeElapsed -= Time.deltaTime;
         if (timeElapsed <= 0)
         {
             SpawnEggs();
             timeElapsed = spawnPeriod;
         }
 
     }
 
     void SpawnEggs()
     {
         Vector2 pos = center = new Vector2(Random.Range(-size.x / 2, size.x / 2), Random.Range(-size.y / 2, size.y / 2));
 
         Instantiate(Eggprefab, pos, Quaternion.identity);
 
     }
 
     void OnDrawGizmosSelected() 
     { 
         Gizmos.color = new Color(1, 0, 0, 0.5f); Gizmos.DrawCube(center, size); 
     }
 }

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

125 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 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 do I randomly create 2 objects, each with a different shape and color? 1 Answer

Spawning Objects 1 Answer

Make Objects spawn within different screen sizes 0 Answers

How Increase And Decrease And Reset Spawn Rate Over 0 Answers

How do i make a "summon" object - and let the player face in its direction? 0 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