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 seppukustl · Jan 28, 2014 at 08:55 AM · c#eventstate-machinecycle

Quick State Machine Question c#

I was following the BZA tutorial for spawning enemies and everything is working great but is there a way (or did I miss something along the way?) to make it cycle and spawn a new enemy after a certain amount of time after the enemy in that spawn point dies?

here is the script at the moment.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ZombieSpawn : MonoBehaviour {
 
     public enum State
     {
         Idle,
         Initialize,
         Setup,
         SpawnMob
     }
 
     public GameObject[] zombiePrefab;
     public GameObject[] spawnPoints; // array holds a ref to all spawnpoints in the world
 
     public State state;    // local variable that holds our current state.
 
 
     void Awake()
     {
         state = ZombieSpawn.State.Initialize;
     }
     // Use this for initialization
     IEnumerator Start () {
         while(true)
         {
             switch(state)
             {
             case State.Initialize:
                 Initialize();
                 break;
             case State.Setup:
                 Setup();
                 break;
             case State.SpawnMob:
                 SpawnMob();
                 break;
             }
 
             yield return 0;
         }
     
     }
     
     private void Initialize()
     {
         Debug.Log ("Initialize Function");
 
         if(!CheckForMobPrefabs ())
         {
             return;
         }
         if(!CheckForSpawnPoints())
         {
             return;
         }
 
         state = ZombieSpawn.State.Setup;
     }
 
     private void Setup()
     {
         Debug.Log("Setup Function");
 
         state = ZombieSpawn.State.SpawnMob;
     }
 
     private void SpawnMob()
     {
         Debug.Log("Spawn Mob");
 
         GameObject[] gos = AvailableSpawnPoints();
 
         for(int cnt = 0; cnt < gos.Length; cnt++)
         {
             GameObject go = Instantiate(zombiePrefab[Random.Range(0, zombiePrefab.Length)],
                                         gos[cnt].transform.position,
                                         Quaternion.identity
                                         ) as GameObject;
             go.transform.parent = gos[cnt].transform;
         }
 
         state = ZombieSpawn.State.Idle;
     
     }
 
     //check to see if we have at least one mob prefab to spawn
     private bool CheckForMobPrefabs()
     {
         if(zombiePrefab.Length > 0)
             return true;
         else
             return false;
     }
 
     //check to see if we have at least on spawn point
     private bool CheckForSpawnPoints()
     {
         if(spawnPoints.Length > 0)
             return true;
         else
             return false;
     }
 
     //generate a list of available spawnpoints that do not have any mobs childed to it
     private GameObject[] AvailableSpawnPoints()
     {
         List<GameObject> gos = new List<GameObject>();
 
         for(int cnt = 0; cnt < spawnPoints.Length; cnt++)
         {
             if(spawnPoints[cnt].transform.childCount == 0)
             {
                 Debug.Log("Spawn Point Available");
                 gos.Add(spawnPoints[cnt]);
             }
         }
 
         return gos.ToArray();
     }
 }
 
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 getyour411 · Jan 28, 2014 at 08:57 AM

Not directly from this - after the Spawn it goes to state Idle which has no supporting function shown. If I had to guess, I'd say the Zombie (on death) is supposed to call back to this State machine and set state to SpawnMob, which would look for a mobspawner with 0 transform children, find one, and spawn it. That could be a timing issue tho, since the Zombie (if it's still an active gameObject, and it would have to be to run its scripts) would still be a child. Look in mob code for OnDeath stuff and see if it clears it up, post back otherwise.

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 seppukustl · Jan 28, 2014 at 01:35 PM 0
Share

Sadly this was the only script I used from the tutorial series, I have my own scripts for everything else, But I'm sure I could add something into my enemy script that would force the state machine to reactivate upon it's death. Thanks for that advice, I didn't even think about that. I'll look into that later and post back with the results.

avatar image seppukustl · Jan 29, 2014 at 02:43 AM 0
Share

Awesome, It's kinda working now, I just need to implement an elapsed time before spawn only when the player is a certain distance away. Otherwise It will be one nonstop spawn fest at the spawn point :P

avatar image getyour411 · Jan 29, 2014 at 02:46 AM 0
Share

Good deal.

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Draw a handle in editor when key is pressed 2 Answers

Having trouble subscribing to events from external class 2 Answers

Serialize UnityEvent for use in EditorWindow 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