Alternative to code this Help.

Hi, I don’t have an idea to do with this script, once I finished writing it I’ve realized I done it wrong.

using UnityEngine;
using System.Collections;

namespace Vuforia
{
    public class SpawnScript : MonoBehaviour
    {
        DectectableScript DS;
        ZoneManagerScript Zms;
        GameObject MonsterPrefab;
        GameObject MonsterReference;

        public Vector3 Zone1;
        public Vector3 Zone2;
        public Vector3 Zone3;
        public Vector3 Zone4;
        public Vector3 Zone5;

        void Start()
        {

        }

        public void SpawnInZone1()
        {   
            GameObject ZMSObject = GameObject.FindGameObjectWithTag ("Zone Manager");
            Zms = ZMSObject.GetComponent<ZoneManagerScript>();

            DS = GetComponent<DectectableScript>();
            MonsterReference = GetComponent<DectectableScript>().Monster;

            if (Zms.Zone1Empty)
            {
                MonsterPrefab = Instantiate(MonsterReference,Zone1,Quaternion.identity) as GameObject;
                Debug.Log("Monster Spawned In Zone 1");
                Zms.Zone1Empty = false;
            }
            if (!Zms.Zone1Empty)
            {
                SpawnInZone2();
            }
        }
        void SpawnInZone2()
        {
            if (Zms.Zone2Empty)
            {
                MonsterPrefab = Instantiate(MonsterReference,Zone2,Quaternion.identity) as GameObject;
                Debug.Log("Monster Spawned In Zone 2");
                Zms.Zone2Empty = false;
            }
            if (!Zms.Zone2Empty)
            {
                SpawnInZone3();
            }
        }
        void SpawnInZone3()
        {
            if (Zms.Zone3Empty)
            {
                MonsterPrefab = Instantiate(MonsterReference,Zone3,Quaternion.identity) as GameObject;
                Debug.Log("Monster Spawned In Zone 3");
                Zms.Zone3Empty = false;
            }
            if (!Zms.Zone3Empty)
            {
                SpawnInZone4();
            }
        }
        void SpawnInZone4()
        {
            if (Zms.Zone4Empty)
            {
                MonsterPrefab = Instantiate(MonsterReference,Zone4,Quaternion.identity) as GameObject;
                Debug.Log("Monster Spawned In Zone 4");
                Zms.Zone4Empty = false;
            }
            if (!Zms.Zone4Empty)
            {
                SpawnInZone5();
            }
        }
        void SpawnInZone5()
        {
            if (Zms.Zone5Empty)
            {
                MonsterPrefab = Instantiate(MonsterReference,Zone5,Quaternion.identity) as GameObject;
                Debug.Log("Monster Spawned In Zone 5");
                Zms.Zone5Empty = false;
            }
            if (!Zms.Zone5Empty)
            {
                Debug.Log("Zones Full");
            }
        }

    }
}

I want the Monster to spawn once. But since it keeps going it spawned 5 times. I just want an idea how to fix this.
Heres the code with the bool if needed.

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

namespace Vuforia
{
    public class ZoneManagerScript : MonoBehaviour

    {
        public bool Zone1Empty;
        public bool Zone2Empty;
        public bool Zone3Empty;
        public bool Zone4Empty;
        public bool Zone5Empty;

        void Start()
        {
            Zone1Empty = true;
            Zone2Empty = true;
            Zone3Empty = true;
            Zone4Empty = true;
            Zone5Empty = true;

        }

    }
}

General assistance in this area (check the post @Kiwasi wrote on coroutines, specifically):

Code I wrote specifically for this kind of spawner, heavily commented:

Well actually, I want the monster to spawn once, but then I also want another monster to spawn with the script. It don’t know how to explain it. Actually I want to something to check if zone 1 is available and if it is spawn it, if not check zone 2 etc. I have no idea or clue to start that.

Or maybe once it spawned stop the rest of the code.

In terms of it being free are you referencing to like distance to the player for say. If no you could also just use Random.Range(0,5) giving you a number from 0 - 4 , put the spawns in an array an assign it to that number you got. if you want to add the distance above to see if it’s available run it through a while loop until you find a random chosen and player is not close enough

If I understood correctly what you want, I noticed a flaw in your code:

public void SpawnInZone1()
        {
            GameObject ZMSObject = GameObject.FindGameObjectWithTag ("Zone Manager");
            Zms = ZMSObject.GetComponent<ZoneManagerScript>();
            DS = GetComponent<DectectableScript>();
            MonsterReference = GetComponent<DectectableScript>().Monster;
            if (Zms.Zone1Empty)
            {
                MonsterPrefab = Instantiate(MonsterReference,Zone1,Quaternion.identity) as GameObject;
                Debug.Log("Monster Spawned In Zone 1");
                Zms.Zone1Empty = false;    //<----- Sneaky sneaky! You set false here
            }
            if (!Zms.Zone1Empty)   //<------ and it will always call this block
            {
                SpawnInZone2();
            }
        }

Basically I would just reverse the order of check:

public void SpawnInZone1()
        { 
            GameObject ZMSObject = GameObject.FindGameObjectWithTag ("Zone Manager");
            Zms = ZMSObject.GetComponent<ZoneManagerScript>();

            DS = GetComponent<DectectableScript>();
            MonsterReference = GetComponent<DectectableScript>().Monster;

            if (!Zms.Zone1Empty)   //<------ and it will always call this block
            {
                SpawnInZone2();
            }
            else
            {
                MonsterPrefab = Instantiate(MonsterReference,Zone1,Quaternion.identity) as GameObject;
                Debug.Log("Monster Spawned In Zone 1");
                Zms.Zone1Empty = false;    //<----- Sneaky sneaky! You set false here
            }
          
        }

Also, your functions are basically all the same, just different values. But before refactoring the function, I would refactor SpawnPoint as well:

public class SpawnScript : MonoBehaviour
    {
        public DectectableScript detector;
        public ZoneManagerScript zoneManager;
        public GameObject MonsterPrefab;
        public SpawnPoint nextSpawnPoint;

        private GameObject MonsterReference;
}

Instead using Vector3, use GameObjects inside scene, add them SpawnScript and link via inspector the next spawnPoint in case this one is used ( also link detectableScript and ZoneManager via inspector):

public class SpawnScript : MonoBehaviour
    {
        public DectectableScript detector;
        public ZoneManagerScript zoneManager;
        public GameObject MonsterPrefab;
        public SpawnPoint nextSpawnPoint;

        private GameObject MonsterReference;

        public void SpawnMonster()
        { 
            if ( MonsterReference != null )
            {
                if ( nextSpawnPoint != null ) nextSpawnPoint.SpawnMonster();
                return;
            }
           
            MonsterReference = Instantiate(detector.Monster,
                                                   transform.position,
                                                   Quaternion.identity) as GameObject;
            Debug.Log("Monster Spawned In "+name);
           
            // we dont need isEmpty since MonsterReference is, well, referenced if alive
        }
}

ZoneManager becomes:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Vuforia
{
    public class ZoneManagerScript : MonoBehaviour
    {
        public SpawnScript[] list;
        void Start()
        {
       
        }
    }
}

My general rule of thumb is whenever you see code duplication, think how to refactor it so you reuse your code as much as you can. Also, always use Arrays or Lists instead naming variables like Spawn1, Spawn2, Spawn3, etc… Imagine if your boss came and said: “Jim, I changed my mind, let’s see how the game works with 1000 Spawn points!” Good luck with that! o_o

Hope it helps!

It sounds like you’re having a conceptual problem. You should think of this in terms of number of associations you want to keep (A must have B and C), and how much information you want the “spawn points” themselves to have.

For instance, let’s say you make an empty GameObject in your scene and call it “Spawner”. That GameObject can then have a script added, call it “SpawnerScript” with a “range” float value, or a sphere collider, etc… which defines the “zone” it’s in charge of- basically the area in which it can spawn a monster. You can have references to the spawned monsters it’s made within that zone right there in that script (a list of GameObjects), along with any logic associated with the spawn point- like an explicit “AddMonster()” function, “GetMonsters()” to return the list of current monsters, a timer for auto-spawning monsters, etc… That’s a perfectly viable method.

On the other hand, you can use the “Manager” approach. You can make a GameObject in your scene who’s whole job it is to manage each of the spawners in the area- and the spawners themselves would just be empty GameObjects that define “positions” in the scene (nothing more than a Transform). You’d probably make a List member on the manager, and then drag each of your spawners into that list in in the inspector. Or, alternatively, you could use FindGameObjectsWithTag in the Start function (and ONLY the start function), and find all objects with the “Spawner” name or tag or w/e. This is essentially what I did with that script I linked.

In my script, the manager controls absolutely everything, because the spawn points are treated as nothing more than positional data. There’s no reason that that has to be the case though. You can put a script on each of the spawn points which holds the monster-spawns it’s made in that area, and then use the manager to access that script when needed. You can then iterate over each of the spawn points (or their “spawner” script components, specifically) and check if their lists are empty (also probably some sort of internal timer that shows WHEN it went empty), and then tell it to spawn a new monster if it is.

You could also skip storing anything on the spawners themselves and just store it as as a new class object in the manager- make a new nested class called “SpawnerData” and store the spawner reference, list of monsters spawned there, range of the spawn area, timers, etc… You’d then have a list of “SpawnerData”, with an entry for each spawn point, right there in the manager. You could iterate over that list whenever you want to check spawns and such.

There are literally a dozen or more ways to manage this, and figuring out which one you want to do is part of the fun of programming :slight_smile:

Thank you so much I figured out a simple way to do this.

using UnityEngine;
using System.Collections;

namespace Vuforia
{
    public class SpawnScript : MonoBehaviour
    {
        DectectableScript DS;
        ZoneManagerScript Zms;
        public GameObject MonsterPrefab;
        GameObject MonsterReference;
        SpawnScript SS;
        public Vector3 Zone1;
        public Vector3 Zone2;
        public Vector3 Zone3;
        public Vector3 Zone4;
        public Vector3 Zone5;
        int SpawnLimit = 0;

        public bool IsInZone1;
        public bool IsInZone2;
        public bool IsInZone3;
        public bool IsInZone4;
        public bool IsInZone5;

        void Start()
        {
            IsInZone1 = false;
            IsInZone2 = false;
            IsInZone3 = false;
            IsInZone4 = false;
            IsInZone5 = false;
        }

        public void SpawnInZone1()
        {   
            GameObject ZMSObject = GameObject.FindGameObjectWithTag ("Zone Manager");
            Zms = ZMSObject.GetComponent<ZoneManagerScript>();

            DS = GetComponent<DectectableScript>();
            MonsterReference = GetComponent<DectectableScript>().Monster;

            if (Zms.Zone1Empty)
            {
                MonsterPrefab = Instantiate(MonsterReference,Zone1,Quaternion.identity) as GameObject;
                Debug.Log("Monster Spawned In Zone 1");
                Zms.Zone1Empty = false;
                SpawnLimit ++;
                IsInZone1 = true;
            }
            if (!Zms.Zone1Empty)
            {
                SpawnInZone2();
            }
        }
        void SpawnInZone2()
        {
            if(SpawnLimit < 1)
            {
                if (Zms.Zone2Empty)
                {
                    MonsterPrefab = Instantiate(MonsterReference,Zone2,Quaternion.identity) as GameObject;
                    Debug.Log("Monster Spawned In Zone 2");
                    Zms.Zone2Empty = false;
                    SpawnLimit ++;
                    IsInZone2=true;

                }
            }
            if (!Zms.Zone2Empty)
            {
                SpawnInZone3();
            }
        }
        void SpawnInZone3()
        {
            if(SpawnLimit < 1)
            {
                if (Zms.Zone3Empty)
                {
                    MonsterPrefab = Instantiate(MonsterReference,Zone3,Quaternion.identity) as GameObject;
                    Debug.Log("Monster Spawned In Zone 3");
                    Zms.Zone3Empty = false;
                    SpawnLimit ++;
                    IsInZone3=true;
                }
            }
            if (!Zms.Zone3Empty)
            {
                SpawnInZone4();
            }
        }
        void SpawnInZone4()
        {   
            if(SpawnLimit < 1)
            {
                if (Zms.Zone4Empty)
                {
                    MonsterPrefab = Instantiate(MonsterReference,Zone4,Quaternion.identity) as GameObject;
                    Debug.Log("Monster Spawned In Zone 4");
                    Zms.Zone4Empty = false;
                    SpawnLimit ++;
                    IsInZone4=true;
                }
            }
            if (!Zms.Zone4Empty)
            {
                SpawnInZone5();
            }
        }
        void SpawnInZone5()
        {
            if(SpawnLimit < 1)
            {
                if (Zms.Zone5Empty)
                {
                    MonsterPrefab = Instantiate(MonsterReference,Zone5,Quaternion.identity) as GameObject;
                    Debug.Log("Monster Spawned In Zone 5");
                    Zms.Zone5Empty = false;
                    SpawnLimit ++;
                    IsInZone5=true;
                }
            }
            if (!Zms.Zone5Empty)
            {
                Debug.Log("Zones Full");
            }
        }

    }
}

Thanks So much. But I figured out another method.

using UnityEngine;
using System.Collections;

namespace Vuforia
{
    public class SpawnScript : MonoBehaviour
    {
        DectectableScript DS;
        ZoneManagerScript Zms;
        public GameObject MonsterPrefab;
        GameObject MonsterReference;
        SpawnScript SS;
        public Vector3 Zone1;
        public Vector3 Zone2;
        public Vector3 Zone3;
        public Vector3 Zone4;
        public Vector3 Zone5;
        int SpawnLimit = 0;

        public bool IsInZone1;
        public bool IsInZone2;
        public bool IsInZone3;
        public bool IsInZone4;
        public bool IsInZone5;

        void Start()
        {
            IsInZone1 = false;
            IsInZone2 = false;
            IsInZone3 = false;
            IsInZone4 = false;
            IsInZone5 = false;
        }

        public void SpawnInZone1()
        {   
            GameObject ZMSObject = GameObject.FindGameObjectWithTag ("Zone Manager");
            Zms = ZMSObject.GetComponent<ZoneManagerScript>();

            DS = GetComponent<DectectableScript>();
            MonsterReference = GetComponent<DectectableScript>().Monster;

            if (Zms.Zone1Empty)
            {
                MonsterPrefab = Instantiate(MonsterReference,Zone1,Quaternion.identity) as GameObject;
                Debug.Log("Monster Spawned In Zone 1");
                Zms.Zone1Empty = false;
                SpawnLimit ++;
                IsInZone1 = true;
            }
            if (!Zms.Zone1Empty)
            {
                SpawnInZone2();
            }
        }
        void SpawnInZone2()
        {
            if(SpawnLimit < 1)
            {
                if (Zms.Zone2Empty)
                {
                    MonsterPrefab = Instantiate(MonsterReference,Zone2,Quaternion.identity) as GameObject;
                    Debug.Log("Monster Spawned In Zone 2");
                    Zms.Zone2Empty = false;
                    SpawnLimit ++;
                    IsInZone2=true;

                }
            }
            if (!Zms.Zone2Empty)
            {
                SpawnInZone3();
            }
        }
        void SpawnInZone3()
        {
            if(SpawnLimit < 1)
            {
                if (Zms.Zone3Empty)
                {
                    MonsterPrefab = Instantiate(MonsterReference,Zone3,Quaternion.identity) as GameObject;
                    Debug.Log("Monster Spawned In Zone 3");
                    Zms.Zone3Empty = false;
                    SpawnLimit ++;
                    IsInZone3=true;
                }
            }
            if (!Zms.Zone3Empty)
            {
                SpawnInZone4();
            }
        }
        void SpawnInZone4()
        {   
            if(SpawnLimit < 1)
            {
                if (Zms.Zone4Empty)
                {
                    MonsterPrefab = Instantiate(MonsterReference,Zone4,Quaternion.identity) as GameObject;
                    Debug.Log("Monster Spawned In Zone 4");
                    Zms.Zone4Empty = false;
                    SpawnLimit ++;
                    IsInZone4=true;
                }
            }
            if (!Zms.Zone4Empty)
            {
                SpawnInZone5();
            }
        }
        void SpawnInZone5()
        {
            if(SpawnLimit < 1)
            {
                if (Zms.Zone5Empty)
                {
                    MonsterPrefab = Instantiate(MonsterReference,Zone5,Quaternion.identity) as GameObject;
                    Debug.Log("Monster Spawned In Zone 5");
                    Zms.Zone5Empty = false;
                    SpawnLimit ++;
                    IsInZone5=true;
                }
            }
            if (!Zms.Zone5Empty)
            {
                Debug.Log("Zones Full");
            }
        }

    }
}