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 /
This question was closed Jan 26, 2021 at 12:17 AM by Longshanks87 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Longshanks87 · Jan 25, 2021 at 12:25 PM · 1st person

How do i make sure the spawners stay random.

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

public class SpawnZombie : MonoBehaviour {

 private Transform PlayerTransform;
 public int zombAmount, WaveNumber, waveReset, WaveIncriment, waveChecker;
 public GameObject[] points;
 public bool reset;
 
 public Text waveText;

 int pointChoice;
 private GameObject chosenSpawner;

 // Start is called before the first frame update
 void Start()
 {
     WaveIncriment = 4;
     PlayerTransform = GameObject.FindGameObjectWithTag("Player").transform;  
     StartCoroutine(CheckRadius());
     StartCoroutine(CheckWave());
     reset  = false;
 }

 void Update()
 {
     waveText.text = ("Wave" + WaveNumber);
    
 }

 private IEnumerator CheckRadius()
 {
   
     yield return new WaitForSeconds(0.5f);

     pointChoice = Random.Range(0, points.Length);
     chosenSpawner = points[pointChoice];

     foreach (GameObject spawner in points)
     {
         if (chosenSpawner)
         {
             var script = chosenSpawner.GetComponent<SpawnPoint>();

             if (PlayerTransform)
             {

                 float dist = Vector3.Distance(PlayerTransform.position, chosenSpawner.transform.position);

                 if (dist >= 40f)
                 {
                     script.hasBegun = false;
                     chosenSpawner.SetActive(false);
                 }

                 if (dist < 40f)
                 {
                     chosenSpawner.SetActive(true);
                     if (script.hasBegun == false && zombAmount < script.waveLimiter)
                     {
                         script.StartCoroutine("SpawnZombieVoid");
                     }

                     script.hasBegun = true;
                 }
             }
         }
     }

     StartCoroutine(CheckRadius());    
 }


 private IEnumerator CheckWave()
 {
     yield return new WaitForSeconds(2);

     if(waveReset <= 0)
     {
     
         if (reset == true)
         {
             WaveNumber++;
             WaveIncriment += 4;
             reset = false;
         }                 
     }
     StartCoroutine(CheckWave());
 }

  

}

======# SECOND SCRIPT

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

public class SpawnPoint : MonoBehaviour {

 private GameObject chosenZombie;
 private int zombChoice;
 public GameObject[] zombiePrefab;
 private SpawnZombie m_SpawnZombie;

 public bool hasBegun;

 public int waveLimiter;

 void Start()
 {
     m_SpawnZombie = GetComponentInParent<SpawnZombie>();
     waveLimiter = 4;
 }
 void Update()
 {
     waveLimiter = m_SpawnZombie.WaveIncriment;

 
 }

 private IEnumerator SpawnZombieVoid()
 {
     yield return new WaitForSeconds(3);
     zombChoice = Random.Range(0, zombiePrefab.Length);

     chosenZombie = zombiePrefab[zombChoice];

     if (hasBegun == true)
     {
         if (m_SpawnZombie.zombAmount <= waveLimiter && m_SpawnZombie.waveChecker <= waveLimiter && m_SpawnZombie.zombAmount < 15)
         {

             GameObject zombie;
             zombie = Instantiate(chosenZombie, transform.position, transform.rotation) as GameObject;
             m_SpawnZombie.zombAmount++;
             m_SpawnZombie.waveChecker++;
             hasBegun = false;

         }

         if (m_SpawnZombie.waveChecker == m_SpawnZombie.waveReset)
         {

             hasBegun = false;
             m_SpawnZombie.reset = true;
             m_SpawnZombie.waveReset = 0;
             m_SpawnZombie.waveChecker = 0;

         }

         hasBegun = false;
         StartCoroutine(SpawnZombieVoid());
     }
     
     
 }

}

This works perfectly if I am within range of the 5 spawn points, The moment I come out of range of one of them the random factor seems to dissappear and they spawn from only the ones that the boolean hasbegun stayed on for. Now I don't want that, I need a way of checking if a spawn point has been turned off and removing it as a factor in the calculation. But the array needs to stay as it is otherwise I'm sure it won't turn back on properly once within range again.

As I said it all works fine to an extent. If I go back into range of all 5 they restart properly and the random choice returns properly. But I want the player to be able to run to another side of the map and not have zombies only coming through one window.

Thanks for any help here It's got me this one. I've.pretty. much self taught everything else but I want this to feel better than it currently does

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 logicandchaos · Jan 25, 2021 at 04:53 PM 0
Share

You could probably solve this by adding another bool to keep track if spawner is on or not, if you don't want to change your script. In program$$anonymous$$g there are many ways of doing the same thing, so you could rewrite this in many different ways, some may be better.

avatar image Longshanks87 logicandchaos · Jan 25, 2021 at 05:49 PM 0
Share

I'll have a look at tracking another boolean in to check for the gameobjects active status, I may even throw in another small coroutine to do this alongside the others. Thanks for the idea. It's been around three days now trying different concepts.

avatar image Longshanks87 logicandchaos · Jan 26, 2021 at 12:19 AM 0
Share

Thanks for the advice. I worked around it by segregating the script, Creating a third coroutine, And an int tracker for how many times a spawner was used, Works perfectly now

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

110 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

Related Questions

3rd person to 1st person view with two engines 0 Answers

In roll a ball the ball go to outside the wall why any one ans me 1 Answer

FMOD FAILED TO INITAILIZE THSI MAY BECAUSE OF YOUR SOUND 1 Answer

How do i make my enemy AI attack? 1 Answer

How to detect what layer is colliding 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