Search Unity

GameObject instantiation not working

Discussion in 'Scripting' started by lottyjackpot, Feb 3, 2020.

  1. lottyjackpot

    lottyjackpot

    Joined:
    Nov 15, 2019
    Posts:
    4
    I'm trying to instantiate a prefab in a loop, but the first time it the instantiate function is called, the code breaks out of the function for no reason. I've used the stepping tools in Visual Studio and can't figure out why it's doing this. I thought it might be because of my version of Unity but I've just updated to 2019.3.0f6 and the problem is still occurring. Does anyone have any suggestions?

    Code (CSharp):
    1. public void GotTickets(FBTicket[] tickets)
    2.     {
    3.         if (ticketHolders.Count < tickets.Length)
    4.         {
    5.             int amountNeeded = tickets.Length - ticketHolders.Count;
    6.             for (int i = 0; i < amountNeeded; i++)
    7.             {
    8.                 TicketHolder ticketHolder = Instantiate(ticketHolderPrefab, ticketHolderParent).GetComponent<TicketHolder>();
    9.                 ticketHolders.Add(ticketHolder);
    10.             }
    11.         }
    12.         for (int i = 0; i < ticketHolders.Count; i++)
    13.         {
    14.             ticketHolders[i].gameObject.SetActive(false);
    15.         }
    16.         for (int i = 0; i < tickets.Length; i++)
    17.         {
    18.             ticketHolders[i].SetTicket(tickets[i].jackpot, tickets[i].numbers);
    19.         }
    20.     }
     
    Last edited: Feb 3, 2020
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Check the Unity console for errors.

    Also note that when you Instantiate an object, it will immediately call Awake on any active components before continuing with the code that did the Instantiation. You may want to check what your prefab might be doing in Awake and whether it will somehow interact with this code. (Although I can't immediately think of anything that would cause that loop to terminate early.)
     
  3. lottyjackpot

    lottyjackpot

    Joined:
    Nov 15, 2019
    Posts:
    4
    Thanks for the response. There aren't any errors in the Unity console and the GameObject that is being instantiated doesn't contain any scripts that include any Awake functions. The weird thing is no errors or exceptions are being thrown when stepping through in VS, it just stops the debugging and breaks the function.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Earlier you said it was breaking out of the loop. If it's breaking out of the entire function then that pretty much has to be an exception of some kind. What's the last line in this loop that it goes to before "breaking", and where exactly does it go next after that?

    You also may want to try rebuilding to ensure that your source code is an exact match for the compiled code that's actually running; you can get very confusing results if that's not the case.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Is it entering your for loops at all? Did you debug your count and length amounts?
     
  6. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    224
    I'd consolidate your multiple for loops into one so its easier to comprehend whats going on.
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Those 3 loops are iterating across different numerical ranges, so combining them probably doesn't make sense.
     
  8. lottyjackpot

    lottyjackpot

    Joined:
    Nov 15, 2019
    Posts:
    4
    The loops aren't my problem, the logic of the function works fine, its whenever instantiate is called it breaks the function but doesn't throw any errors.
     
  9. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    224
    Its probably this, try putting the breakpoint on line 9 and see if it reaches it. Or be more explicit with why you think its breaking out of the loop

    You're right
     
  10. lottyjackpot

    lottyjackpot

    Joined:
    Nov 15, 2019
    Posts:
    4
    Thanks for the replies guys, but I've found a work around for this, I suspect it was causing the function to break as it was being called as a callback from getting data from a Firebase database using their API. I have no idea why this was happening but that was the cause. Also regarding the three loops. The first is to ensure there are enough ticketHolders for each ticket, the second disables all ticketHolders from the scene, and the last sets each ticket to a ticketHolder, the logic works fine
     
  11. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Glad you got something working.

    If this was being called asynchronously, I can think of two ways that might have caused problems:
    • Most stuff in the UnityEngine namespace is only safe to use in the main Unity thread. If this was running on another thread, then Instantiate probably couldn't work at all.
    • Asynchronous systems handle exceptions a bit differently, and so if this did throw an exception it might not have shown up in the console.
    In the future, when you get weird problems with asynchronous functions, you might consider putting the logic into a simple synchronous test case to see whether that changes anything.