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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Question Callback for PhotonNetwork.Instantiate

Discussion in 'Multiplayer' started by siddharth3322, Dec 18, 2021.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,034
    I was working on the multiplayer game in which a maximum of 2 players was allowed to join in the same room and for this purpose, I was using Photon Network.

    Now as per my requirements I was spawning symbols using PhotonNetwork.Instantiate() method and here you have my actual code:
    Code (CSharp):
    1. if (PhotonNetwork.IsMasterClient)
    2.         {
    3.             foreach (Transform placeHolder in player1PlaceHolders)
    4.             {
    5.                 for (int i = 0; i < 2; i++)
    6.                 {
    7.  
    8.                     int placeHolderSize = placeHolder.GetComponent<SymbolPlaceHolder>().symbolSize;
    9.                     GameObject playerSymbolObj = PhotonNetwork.Instantiate("Photon/" + player1SymbolsPrefs[placeHolderSize - 1].name, placeHolder.position, Quaternion.identity);
    10.                     playerSymbolObj.transform.SetParent(placeHolder);
    11.                     placeHolder.GetComponent<SymbolPlaceHolder>().symbolObjList.Add(playerSymbolObj);
    12.                     placeHolder.GetComponent<SymbolPlaceHolder>().UpdateAvailaleSymbolsText();
    13.  
    14.                     player1SymbolObjList.Add(playerSymbolObj);
    15.                 }
    16.             }
    17.         }
    Now if you notice, I was adding spawned Symbol within SymbolObjList which was working perfectly for the Host player but not with another Client player.

    As per my game requirements, I want my SymbolObjList to be filled with symbols of Host and Client players on each side.

    For Host player, it is already filling but for Client Player, I want some callback so I can add items in the list for Client Player.

    Please suggest to me something on this matter.
     
  2. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Are these symbols just a scene object, or the actual player, if it's just a scene object use PhotonNetwork.InstantiateRoomObject or PhotonNetwork.InstantiateSceneObject. Im not entirely sure which one was the correct one but give it a shot
     
    siddharth3322 likes this.
  3. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,034
    Actually, these symbols are player objects, I was working on a kind of Tic Tac Toe type of game.
    So both players have different X and O symbols to pick and place within the grid board. I need these symbols with PhotonView attached but I want some callback after these symbols Instantiated within the game so I can do some necessary stuff with these objects.
     
  4. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,034
    After some research, I found IPunInstantiateMagicCallback interface of GameObject instantiate callback.
    Source Link
    https://doc.photonengine.com/zh-cn/pun/current/getting-started/dotnet-callbacks

    For this to work, I have to register and unregister through these lines:
    PhotonNetwork.AddCallbackTarget(this)
    PhotonNetwork.RemoveCallbackTarget(this)

    But still, I can't able to get success in getting some event when PhotonNetwork.Instantiate() method called.
     
  5. icefallgames

    icefallgames

    Joined:
    Dec 6, 2014
    Posts:
    75
    Your link explicitly says that you don't have to register a callback target for IPunInstantiateMagicCallback.

    Just implement IPunInstantiateMagicCallback on one of the MonoBehaviors attached to the object being created.
     
    siddharth3322 likes this.
  6. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,034
    Shall I require to add this interface IPunInstantiateMagicCallback to the object which is spawning?
    Or in any controller kind of script, I can add this interface for the callback.