1
\$\begingroup\$

I have a game where there are about 4 empty objects that are my object "spawners". I have two scripts attached to all of them, one that will keep spawning objects from "basictable", and then switch to the other component which will then spawn objects from "complextable".

This is my first script :

public void SpawnA() {

    if (destroyer.spawncount < destroyer.spawnchange) { //if condition

        int randomi = Random.Range (0, basictable.Length);
        Instantiate (basictable[randomi], new Vector3 (this.transform.position.x,this.transform.position.y,basictable[randomi].transform.position.z), Quaternion.identity);
        Invoke ("SpawnA", Random.Range (mini, maxi));

    }
    else if (keep){
        this.GetComponent<spawnscript2>().SpawnB();
        keep = false;
    }

And the second script :

public void spawnB()  {

        int randomi = Random.Range (0, complextable.Length);
        bobo = (GameObject) Instantiate (complextable [randomi], new Vector3 (this.transform.position.x, this.transform.position.y, complextable [randomi].transform.position.z), Quaternion.identity) as GameObject;
        StartCoroutine ("DelayedSpawnC");

}

IEnumerator DelayedSpawnC () {

        yield return  new WaitForSeconds (6);
        destroyer.spawncount = 0;
        this.GetComponent<basicSpawner> ().keep = true;
        this.GetComponent<basicSpawner> ().SpawnA();
}

Logically, this should work. And indeed it does. But the problem I'm having is that my "spawner", sometimes works, and sometimes doesn't, some objects from the complextable are always spawned, but in other cases the spawner just does nothing, until it switches to the first script. Is there anything I can do to optimize the spawning process and avoid this? I can't get ahold of why are some blocks spawned and others take much longer, while other blocks don't get instantiated at all.

\$\endgroup\$
1
\$\begingroup\$

First off, there's inconsistency in your "DelayedSpawn" method name. When you call StartCoroutine its called "DelayedSpawn", but the coroutine method seems to be named "DelayedSpawnC".

Second, your "spawnB" method seems to select an object at random and the Z position is dependent on which object it chose. According to your description, sometimes it works and sometimes now, so it makes sense that the random is somehow related. Could be that some of the objects in the complextable are empty, null, or their Z position is such that you can't see them.

Last, the code seems to me way more complex than it should be. Just put a boolean telling you if you need to use spawnA or spawnB (or better yet, use a delegate to point to the correct method), and then within the Update() method call the relevant methods for spawning.

That's as much as I can help with the information you provided. Good luck.

\$\endgroup\$
  • \$\begingroup\$ For the DelayedSpwn it's an error I made when I pasted the code here and made a few edits. I apologize! And for the complextable, I don't have any null objects.. And I'll try to do as you suggested. The problem is that when I was using the Update() method, the functions would be called several times, I only want it to be called one time. \$\endgroup\$ – Zee Apr 26 '15 at 13:36
  • \$\begingroup\$ Paste the update code and we'll see why it's called more than once \$\endgroup\$ – tbkn23 Apr 26 '15 at 13:54
  • \$\begingroup\$ I don't have an update anymore because of that, and it was just like this one. Like this : void Update() { if(boolvar) { spawnB(); boolvar = false; } } But because of the nature of Update, even if I make the variable false right inside the if statement, it would update too fast, causig it to spawn the object 5 or 6 times before actually realizing it shouldn't. Even FixedUpdate wouldn't work. \$\endgroup\$ – Zee Apr 26 '15 at 14:04
  • \$\begingroup\$ Thats not true, Updates in unity are Synchronous, the next update (or the update of the next object) will not run until the first one is complete. Your problem is probably that you don't want to spawn one every frame. You could use a coroutine, but put them all in a single coroutine, the jumping around between coroutines like you do seems a bit overcomplicated and is a recipe for bugs, like the one you have. \$\endgroup\$ – tbkn23 Apr 26 '15 at 14:06
  • \$\begingroup\$ Hmm, what you say is logic, but what happens is far from being logical. Right now I just realized that : Instantiate (xxxx); StartCoroutine("Delay"); The program would actually go to the delay, wait for x seconds, and THEN come back and instantiate the object. How logical is that? \$\endgroup\$ – Zee Apr 26 '15 at 14:24

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.