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 Aiden James · Jul 14, 2014 at 06:55 PM · gameobjectprefabspawnzombie

Help? Im trying to get my zombie prefab to spawn, but stop them spawning after a set spawn limit.

Here is my code, i cant seem to figure out whats wrong. Thanks in advance!

 #pragma strict
 
 var zombiePrefab : GameObject; // Prefab to spawn
 var zombiesSpawned : int; //Zombies Spawned
 var Player : GameObject; //player
 var spawn : boolean;
 var minWait : int;
 var maxWait : int;
 var waitTime : int;
 var spawnLimit : int;
 
 function Start () {
 minWait = 1;
 maxWait = 10;
 waitTime = Random.Range(minWait, maxWait);
 spawn = true;
 }
 
 function Update () {
 if(spawn) 
 {
     Spawn();
 }
     if(zombiesSpawned < spawnLimit)
     {
         spawn ();
     }
     else
     {
         spawn = false;
     }
 }
 
 function Spawn() {
     Instantiate(zombiePrefab, transform.position, transform.rotation);
     zombiesSpawned +=1;
     NewWaitTime();
     spawn = false;
     SetSpawn();
 }
 
 function SetSpawn() {
     yield WaitForSeconds (waitTime);
     spawn = true;
 }
 
 function NewWaitTime() {
 waitTime = Random.Range(minWait, maxWait);
 }
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 Kiwasi · Jul 14, 2014 at 06:56 PM 0
Share

Your code formatting for one. I got it for you this time

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiwasi · Jul 14, 2014 at 07:08 PM

You've got some weird stuff going on here.

First delete lines 21 to 24. These lines will attempt to spawn a zombie every time spawn is true, regardless of the number of zombies that are present.

2nd up you really need two bools to manage this. You should use one to manage the timer and another to manage the spawn limit.

An even better way would be to manage the whole thing through a single coroutine. The advantage of this is there is no need to check any variables each frame. In fact once you have finished spawning there is no overhead. Here is some pseudo code you can use. Might be a couple of errors as I don't usually do JavaScript.

 #pragma strict
  
 var zombiePrefab : GameObject; // Prefab to spawn
 var minWait : int = 1;
 var maxWait : int = 10;
 var spawnLimit : int;
  
 function Start () {
     spawn ();
 }
  
 function Spawn() {
     var zombiesSpawned : int; //Zombies Spawned
     while (zombiesSpawned < spawnLimit){
         Instantiate(zombiePrefab, transform.position, transform.rotation);
         zombiesSpawned +=1;
         yield WaitForSeconds (Random.Range(minWait, maxWait));
     }
 }

Comment
Add comment · Show 1 · 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 Aiden James · Jul 15, 2014 at 06:21 AM 0
Share

Thank you for the reply but I'm still having some issues. Its telling me, "Its not possible to invoke the expression type boolean." Any help you can give me?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Turning a menu on and off onclick in hololens 0 Answers

Why don't the Prefabs in my GameObject appear? 1 Answer

Setting public GameObject to a different Prefab through code 0 Answers

How to spawn prefab only in allowed area? 0 Answers

Objects flickering between dark and light 7 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