Search Unity

Prefab Instantiation problem.

Discussion in 'Editor & General Support' started by dmitche3, May 16, 2014.

  1. dmitche3

    dmitche3

    Joined:
    Apr 3, 2014
    Posts:
    23
    First off let me say that I'm still new to Unity.
    I have a problem with a bit of code that I don't understand how to solve the problem.

    When a player connects to the server I instantiate the "NetworkPlayer". That object contains several components, one of which I'm using to store information about the player called 'clientInfo".

    In a script that is not associated with NetworkPlayer I'm searching for all "NetworkPlayers" and referencing the 'clientInfo'.
    My problem is that I find the NetworkPlayers without problem but from time to time the 'clientInfo" component has not been instantiated until later, after the code has failed with null references.

    I put break points into the 'clientInfo's Start(), Awake(), and Update() and I can see that the component is not being instantiated.

    My question is how can I assure that a prefab it complete loaded and instantiated before another object tries to reference it?
    And/or, any suggestions as to what could cause a prefab to 'partially instantiate'?
     
  2. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    945
    i would imagine that network delay and things like that cause a delayed instantiate too.
    You could try letting the playerobject send its clientinfo to the other script when it is ready filling in its values.
    possibly via sendmessage if the other script is on an active GameObject, or access it as a singleton.
    But i dont know what this info is and whether there could even be a better approach to storing it.
     
  3. dmitche3

    dmitche3

    Joined:
    Apr 3, 2014
    Posts:
    23
    Thanks ValooFx. But no. I'm running both the client and server locally. But you are on the right path. I did figure out that it is only occurring when debugging and that the debugger is causing the instantiate to not finish before messages are being processed.

    I neglected to mention some parts as the complexity of the project is well... complex as it involves the client, a Unity Server, and a game server. It is the game server that is sending data back to the unity server and then onto the client.

    I'll have to think your tip over as it is a good one. I think that I'll do that as any other method that I can think of will break every time that I put a break point before the game object finishes initializing. And since there is no way of determining how long I may sit examining a break point. Now to figure out what the last object (or close to the last object) in the game object is that gets instantiated. :(

    A better solution (if Unity will allow me to ) is to initialize the classes at declaration rather than the CTOR. That way when the component is created I think that I'll be assured that the members are constructed/instantiated.
     
    Last edited: May 17, 2014
  4. dmitche3

    dmitche3

    Joined:
    Apr 3, 2014
    Posts:
    23
    Yea. By initializing my Dictionaries, List<>s etc at declaration within the class (script) and not using the constructor the problems went away.

    Thanks ValooFx.