0
\$\begingroup\$

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;
        }
    }

}
\$\endgroup\$
  • 2
    \$\begingroup\$ the script seems fine. I think there is another gameobject in your scene that is the reason for your problem \$\endgroup\$ – user125704 Mar 13 '19 at 11:45
  • 3
    \$\begingroup\$ Is this script attached to the object you’re spawning? \$\endgroup\$ – JeffUK Mar 13 '19 at 13:32
  • \$\begingroup\$ 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; \$\endgroup\$ – Chan Ming Mar 14 '19 at 1:14
  • \$\begingroup\$ 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? \$\endgroup\$ – lilKriT Mar 14 '19 at 4:53
  • \$\begingroup\$ 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 \$\endgroup\$ – Chan Ming Mar 14 '19 at 9:17
-1
\$\begingroup\$

remove line from start line to prevent start loop upon spawn also you might have to place the codes seperately

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Can you elaborate on what "placing the codes separately" might look like? \$\endgroup\$ – DMGregory Mar 26 '19 at 20:00
  • \$\begingroup\$ because my gameobject has the spawn manager script attached, which makes it a spawn loop. \$\endgroup\$ – Chan Ming Mar 28 '19 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.