# Random range issues

i tried to make a random spawn system, but when i tried to start it, it spawns more than 1 prefab, which is not intended as i wanted to spawn only one by one.

tried some method to prevent from spawning non stop, but can't really make it to spawn only one.

EDIT: deleted the random range in the start section, it works as intended at the beginning, however when i enabled the same script in the prefab( based on the gameobject listed), it just spawn endlessly, script for these gameobject will be added, and since they are pretty much the same thing, i'll only attach one of them.

  public GameObject Tap, Up, Down, Left, Right;
int WhatToSpawn;
public bool isFree=false;
public bool destroyed=false;

// Use this for initialization
void Start () {
isFree = true;
destroyed = true;

}

// Update is called once per frame
void Update()
{
if (isFree && destroyed)
{
destroyed = false;
isFree = false;
WhatToSpawn = Random.Range(1, 6);

switch (WhatToSpawn)
{
case 1:
Instantiate(Tap, Tap.transform.position, Tap.transform.rotation);
break;
case 2:
Instantiate(Up, Up.transform.position, Up.transform.rotation);
break;
case 3:
Instantiate(Down, Down.transform.position, Down.transform.rotation);
break;
case 4:
Instantiate(Left, Left.transform.position, Left.transform.rotation);
break;
case 5:
Instantiate(Right, Right.transform.position, Right.transform.rotation);
break;
}
}

}


Here's the another script that is made for gameobject, i tried to make conditions that the script will spawn random stuff when destroy but failed to.

    private Swipe swipe;
private SpawnManager spawn;

private void Awake()
{
swipe = GetComponent<Swipe>();
spawn = GetComponent<SpawnManager>();
}

// Update is called once per frame
void Update () {
if (swipe.Tap)
{
Debug.Log("Passed!");
spawn.isFree = true;
Destroy(gameObject);
spawn.destroyed = true;
}
else
{if (swipe.SwipeLeft || swipe.SwipeDown || swipe.SwipeRight || swipe.SwipeUp)
{
Debug.Log("Failed...");
spawn.isFree = true;
Destroy(gameObject);
spawn.destroyed = true;
}
}

}

• the script seems fine. I think there is another gameobject in your scene that is the reason for your problem – user125704 Mar 13 '19 at 11:45
• Is this script attached to the object you’re spawning? – JeffUK Mar 13 '19 at 13:32
• yes, but i've make it to the state where it'll requires some sort of action to respawn it Here's the code if (swipe.SwipeRight) { Debug.Log("Passed!"); spawn.isFree = true; Destroy(gameObject); spawn.destroyed = true; – Chan Ming Mar 14 '19 at 1:14
• Is it possible that you added this component to your object twice? Or that you added this component to two objects and you forgot about one? – lilKriT Mar 14 '19 at 4:53
• but if i only add it to one component only, it only spawn once which is not intended, what i want it to keep spawning one by one after a certain action – Chan Ming Mar 14 '19 at 9:17