Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

NetworkIdentity objects readyness problem

Discussion in 'Multiplayer' started by cephalo2, May 23, 2016.

  1. cephalo2

    cephalo2

    Joined:
    Feb 25, 2016
    Posts:
    257
    So the flow goes like this, You choose to host a game and then you are taken to a screen where a random gameboard is generated. It automatically generates an initial board, but you can press a button to gen more if you are not satisfied with the original.

    Since the gameboard has a NetworkIdentity component, during the event handler to the 'host' button the gameboard has not spawned yet, so the initial board generation fails. I'm calling NetworkManager.StartHost() before I gen the map, but I guess it spawns in another thread and is still not ready when I call the generation routine. I works when I press the button to gen more boards, but not the initial time.

    Is there an easy way to now if an object is spawned and ready? I would like to generate an initial game board as soon as the game starts without requiring an additional button press. How should I handle this?
     
  2. FeedorFeed

    FeedorFeed

    Joined:
    Jan 9, 2014
    Posts:
    35
    Couldn't you just call the function that the button calls when you press it on scene load?
     
  3. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,102
    Well you can store a reference to your object in scene and check if it's enabled and active or not and when it is, it's ready.
    Do this in a while loop in a coroutine 10 times a second or something like that.
     
  4. cephalo2

    cephalo2

    Joined:
    Feb 25, 2016
    Posts:
    257
    I suppose I could generate a map during the boards 'Start' function. That would probably work.
     
  5. cephalo2

    cephalo2

    Joined:
    Feb 25, 2016
    Posts:
    257
    What I ended up doing is writing my own class that inherits from NetworkManager, and overriding the OnServerReady function to fire off an event. That seems to work as by this time the board has spawned.