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. Dismiss Notice

Third Party Photon RPC does not work

Discussion in 'Multiplayer' started by Jazumn, Mar 22, 2021.

  1. Jazumn

    Jazumn

    Joined:
    Jan 25, 2021
    Posts:
    23
    Hello, beginner question but I do not understand. I get the ...not set to an instance of an object...error for the line

    view.RPC("TakeTimeDamageRPC", RpcTarget.All);

    I know what that in theory means but I have no idea why this happens. So here is the simple script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using Photon.Pun;
    6.  
    7. public class Timer : MonoBehaviour
    8. {
    9.     public float time = 60;
    10.     public Text timeDisplay;
    11.     PhotonView view;
    12.  
    13.     public void TimeDamage()
    14.     {
    15.         view.RPC("TakeTimeDamageRPC", RpcTarget.All);
    16.     }
    17.  
    18.  
    19.     [PunRPC]
    20.     void TakeTimeDamageRPC()
    21.     {
    22.         time -= 1 * Time.deltaTime;
    23.         timeDisplay.text = time.ToString();
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         TimeDamage();
    29.     }
    30. }
    And here are Screenshots how it looks (Time Text is just a text on a Canvas) - so why is it not set to the instance of an object :(?

    timer.PNG
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    To me, this looks as if view is never set properly. Have a look at MonoBehaviourPun and set the view, or inherit from MonoBehaviourPun and use this.photonView to call the RPC.
     
    Jazumn likes this.
  3. Jazumn

    Jazumn

    Joined:
    Jan 25, 2021
    Posts:
    23
    Thank you, that worked perfectly.
     
  4. Jazumn

    Jazumn

    Joined:
    Jan 25, 2021
    Posts:
    23
    One question I I can, it seems like time.deltatime is not working properly or at least the time is much faster then it shoud? But from what I see, it is used also for Photon?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using Photon.Pun;
    6.  
    7. public class Timer : MonoBehaviourPun
    8. {
    9.     public float time = 60f;
    10.     public Text timeDisplay;
    11.     PhotonView view;
    12.     GameMaster gmaster;
    13.  
    14.  
    15.     public void TimeDamage()
    16.     {
    17.         gmaster = FindObjectOfType<GameMaster>();
    18.         this.photonView.RPC("TakeTimeDamageRPC", RpcTarget.All);
    19.     }
    20.    
    21.     void Start()
    22.     {
    23.  
    24.     }
    25.  
    26.     [PunRPC]
    27.     void TakeTimeDamageRPC()
    28.     {
    29.         time -= 1 * Time.deltaTime;
    30.         timeDisplay.text = time.ToString("F0");
    31.  
    32.         if (time <= 0)
    33.         {
    34.             time = 0;
    35.         }
    36.  
    37.         if (time <= 1)
    38.         {
    39.             gmaster.source.Play();
    40.             gmaster.EndTurn();
    41.             time = 60f;
    42.         }
    43.  
    44.     }
    45. }
    46.  
    Code is then called in the update in another script...
     
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    Time.deltaTime is a value from Unity. It tells you how much time passed since the last Update() call by the engine.
    What are you expecting?
     
  6. Jazumn

    Jazumn

    Joined:
    Jan 25, 2021
    Posts:
    23
    Before using Photon - so in one client only and only with Unity code - the countdown counted in seconds. Now that I moved everything to MP, it's much faster, and that does not only look a bit stupid, at least when using a build and the unity game scene it gets asynchron pretty fast, so I am not sure how to set it back to exactly 1 second
     
  7. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    Not knowing your project, it's a bit hard to help.
    I noticed that the RPC method is not a Coroutine. It will be called once via the photonView.RPC() and that's it.
     
  8. Jazumn

    Jazumn

    Joined:
    Jan 25, 2021
    Posts:
    23
    of course, I understand! I just noticied something odd - when I just a room and start the game, the time counts down normally. but when I open a room and join in both with a standalone client and in unity, it gets faster. I would suppose I call everything...2 times and would nee something like an onhostonly?
     
  9. Jazumn

    Jazumn

    Joined:
    Jan 25, 2021
    Posts:
    23
    that of course didnt work - or it did but then only worked on the master client and nothing happened on the other client, lol

    I am not sure about the coroutine - I think the problem is that the countdown counts normal (1 second at a time) when I only connect one player, but then gets faster when the second player joins (I would suppose it gets double as fast). so I think the solution would be calling it for both master client and second client, but then it should only "run" on the master client but also synching on the other...oh man
     
  10. shieldgenerator7

    shieldgenerator7

    Joined:
    Dec 20, 2015
    Posts:
    39
    Instead of counting down time by subtracting using
    Time.deltaTime
    , set a
    timerStartTime
    to
    Time.time
    at the beginning, and then use
    countDownDuration - (Time.time - timerStartTime)
    to calculate how much time is left.

    timeLeft = countDownDuration - (Time.time - timerStartTime);