- Home /
Why Does My GameObject Not Spawn Anymore?
Why does my gameobject not spawn if I don't grab it? The other gameobjects spawn just fine even after not grabbing it, but not this certain one. The gameobjects are all constantly spawning, except the one that doesn't respawn after not grabbing it because it's supposed to spawn only after I collect 10 of a different gameobject(I hope you understood that). So what could be the problem? Here's my GameObject script(this script spawns both the gameobjects I need to collect and the one that doesn't respawn): http://pastebin.com/RuU9UrDj
Now I have a pretty good feeling that it's defiantly the script that I posted that's the problem. I tested it with a different spawn script and the gameobject respawn/kept spawning even after not grabbing it. I know this is pretty a vague explanation and question, so I'm sorry. Anyways I hope someone can help me see what's the problem here.
does the code even get into Instantiate(bigcoin, bigspawn.transform.position, Quaternion.identity); ;
I hope this the GameObject THat doesnt spawn?
Yeah the bigcoin is the one that stops spawning. To answer your first question, yes the code does get into that line, but only until the destroyer destroys it. After the gameobject gets destroyed, it stops working. The debugs no longer appear after it destroys the bigcoin gameobject.
So the Debug.Log("I Love You"); appears when I collect 10 of a different gameobject, the Debug.Log("You Love $$anonymous$$e"); appears when the bigcoin power up spawns, and on the destroyer script the Debug.Log("Z"); appears when it destroys the bigcoin. After "Z" appears none of the debugs appear again. Only if I don't let bigcoin get destroyed will it work, so what the heck?
Bump. This is either a bug or my code is missing something. This could add some challenge to my game though.
I feel we have something in common here, will have a look tonight and see what I can find.
Or you could post the problem scripts here and I could look sooner.
Hmm, problem scripts? Do you mean the ones that don't work correctly(not appearing on the console)? Not sure what you mean here, but the only one that doesn't seem to work correctly is the GameObject/Spawn script that's supposed to spawn the bigcoin gameobject, but it doesn't after it get's destroyed. Here it is: http://pastebin.com/RuU9UrDj
To sum it up, the script makes you collect 10 of a different gameobject tagged as Item1, and after you collect 10, it spawns in the gameobject tagged as Coin(which is the one that doesn't spawn again after getting destroyed). Hope you understand.
Answer by meat5000 · Aug 05, 2014 at 09:30 PM
At a glance, you need to pick up a "Coin" before spawned = false; If you havent done this the instantiation is not invoked. Not sure if Im just describing your intended behaviour here.
But this:
&& timer == 0
Could be a problem too. How about:
&& timer <= 0.0f
Interesting, I didn't notice that those could be a problem. I'll see if that works. Thanks meat.
No problem. The trouble with timer == 0 is that trying to evaluate a float as smack on 0 is very unlikely. So as its a float, evaluate it as a float, evaluate it equal or below and stick the f on the end so it doesnt think its a double or something.
I never knew that, thanks for the information. So I tried it, it doesn't seem to affect the spawning. The gameobject tagged as coin still doesn't spawn after it get's destroyed. So it seems like that wasn't the problem, so maybe it's the spawned = false?
I think if that really is the problem, then there are a couple of ways I might be able to fix it. I can post them but first I'll try them out, then tell you my results.
By the way, the spawned = false is there because without it the Coin won't respawn even if I pick it up. With it, it makes the coin respawn after I collect 10 more "Item1", and it keeps doing it until it get's destroyed by the destroyer that's following me off screen behind me. That's when it doesn't respawn.
destroyed by the destroyer that's following me off screen behind me. That's when it doesn't respawn.
So destroyed out of sequence?
Answer by Foose · Aug 08, 2014 at 06:12 AM
Your problem is, that you destroy the parent game object and with it your whole script. So after u have destroyed it, the script doesnt exist anymore. You should just make a parent gameobject with the destroyer script and let the to be destroyed items spawn as children into it. Than call the destroy function for children and not for the parent. Should solve your problem
Hmm this could work, problem is, is that now that I've looked into it more, it seems the actual problem is with the script spawning the gameobject and not the destroy script. Even if it doesn't get destroyed, it still won't re-spawn after I collect 10 "Item1" tagged gameobjects. By the way, did you test it? Your solution? I'm just curious(seriously I am).