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 Sirevix · Apr 29, 2016 at 02:12 PM · instantiateupdatespawn

spawn every frame, and i want it to spawn only once.

Hello. so, im trying to make a game where you kill an enemy and you get 1 point.

i have a cube (spawner like in minecraft), and this cube spawns an enemy every 3 secounds

(that methot i fount is easyer)

and i want to spawn a new cube/Spawner when you reach 10 points, then when you reach 20 points, then at 50 points and so on...

but i have a problem...

i run the instanciate thing in "void Update"

and when i reach 10 points it will start to spawn a cube every frame and every cube spawns an enemy every 3 secounds and in 3 secounds i'll have like 10000 enemyes

at first i was laughting at what i did, it was funny and every thing, but im stuck with this problem fro 2 days and it's not funny anymore im noob at scripting, just trying to do some game so i will learn while creating something...

here is my code

 using UnityEngine;
 using System.Collections;
 
 public class Score : MonoBehaviour {
 
 
     public float count = 0.0f;
 
     // Use this for initialization
     void Start () {
 
 
 
     }
 
 
 
 
     
     // Update is called once per frame
     void Update () {
         
 
         if (count == 10) {
 
             print ("lol");
 
             GameObject spawnerInstance = Instantiate (Resources.Load ("Cube (1)")) as GameObject;
         }
 
 
     }
 
     void OnGUI () {
         GUI.Label (new Rect (48, 10, 100, 30),"Kills = " + count);    
     }
 
 
     void OnCollisionEnter(Collision collision) {
 
         if (collision.gameObject.tag == "bullet") {
 
 
 
             //Destroy(collision.gameObject);
             //count += 1;
         }
 
     }
 
 }


can anyone help me please? also, im very noob jo if you just keep it simple with the explanation, i'll be very happy :D Thanks :D

Comment
Add comment · Show 1
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 rluks · Apr 29, 2016 at 02:15 PM 0
Share

increase the count variable when you instantiate

1 Reply

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

Answer by Rixterz · Apr 29, 2016 at 02:25 PM

You're checking if count has changed... but you never change it.

Change GameObject spawnerInstance = Instantiate (Resources.Load ("Cube (1)")) as GameObject;

to GameObject spawnerInstance = Instantiate (Resources.Load ("Cube (1)")) as GameObject; count ++;

That will increase the count, so it increases by 1 every time Instantiate is run. Also, you should change your declaration of count to:

 int count = 0;

Because count can never be something like 2.7

Hope this helps

Comment
Add comment · Show 3 · 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 Sirevix · Apr 30, 2016 at 11:03 AM 0
Share

Thank you a lot :D it worked

so for me to understand when i reach 10, it spawns the cube then adds +1 to my score so it won't have to spawn another cube right?

avatar image Rixterz Sirevix · Apr 30, 2016 at 11:06 AM 0
Share

Oh actually you need to change if (count == 10) { to if(count < 10){

That means it will keep spawning cubes until 10 have spawned, and then it will stop

avatar image Rixterz Sirevix · Apr 30, 2016 at 11:08 AM 0
Share

So the code inside update would be

 if (count < 10) {
  
              print ("lol");
  
              GameObject spawnerInstance = Instantiate (Resources.Load ("Cube (1)")) as GameObject;
 //spawn one cube
 count++ ; //tell the game you've spawned another cube
          }
 //optional
 else
 {
 //10 cubes have spawned, so do this code every frame afterwards
 }

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

47 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

Related Questions

How should i add raycast to multiple game objects??? 0 Answers

How to Instantiate prefabs with function Update? 1 Answer

How do I spawn an object where my Player is currently positioned at, and make it disappear over time? Something like cracks on a ground. 1 Answer

Spawning objects on trigger does not work as expected 2 Answers

Having Trouble with Instantiating an object on an axis 2 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