Search Unity

Can I have too many instantiates ?

Discussion in 'Scripting' started by HowWeLife, Oct 8, 2015.

  1. HowWeLife

    HowWeLife

    Joined:
    Jul 4, 2015
    Posts:
    51
    So I made my game and everything is working perfectly, except that sometimes. (still can figure out why) When your suppose to lose, instead of losing the game keeps going. (im thinking that sometimes the lose function I made is not called.) Im trying to figure out whats causing it (but since it rarely happens its hard to tell why ). Last time it happened i noticed there was some lag right before it happened (for a split second.) So is it possible that I am instantiating to many gameobjects and destroying to many and therefore sometimes the code doesn't execute properly?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    That's probably not your issue. Unity doesn't get 'bogged down' and then just not execute functions; when it gets bogged down it just gets slow. Most likely there's a flaw in your losing-game logic; if you could post that, we could check it out for 'holes' that some situations may slip through.

    If you can, it's a good idea to avoid a bunch of instantiations (search for "object pooling" for more information), and if you're in a situation where instantiating stuff causes slowdowns you should consider it; but that's for improving speed, not for situations like this.
     
    HowWeLife and Kiwasi like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can break your game with just on Instantiate.

    Code (CSharp):
    1. public class MyThing : MonoBehaviour{
    2.     void Start () {
    3.         Instantiate (gameObject);
    4.     }
    5. }
    But generally the number of Instantiate calls is a performance issue, not a game breaking issue. (But don't try and clone minecraft by instantiating cubes).
     
    HowWeLife likes this.
  4. HowWeLife

    HowWeLife

    Joined:
    Jul 4, 2015
    Posts:
    51
    Its not a minecraft game its more of a tap the tiles kind of game haha. I'll review the code and if i don't find anything ill post it, but good to know that instantiate isn't the problem.

    ps: after more testing on my phone i realized that sometimes the app just relaunches by itself. Any ideas why something like that can occur?
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Again, probably faulty logic somewhere.
     
  6. HowWeLife

    HowWeLife

    Joined:
    Jul 4, 2015
    Posts:
    51
    Ok so i think i fixed the glitch, however i still don't know why the game would just relaunch by itself(doesn't happen often but still something i should fix it). I don't even have any code that relaunches the app so i don't think its game logic. Any ideas ? Could it be my phone specs can't handle the game so it quits?
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If you run out of memory the game may quit, but (at least on every platform I've used) it would just drop you back to the homescreen, it would not relaunch.