Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Bug in my code

Discussion in 'Multiplayer' started by Jakopro, Jan 21, 2023.

  1. Jakopro

    Jakopro

    Joined:
    May 30, 2021
    Posts:
    18
    I'm making a multiplayer 2D game. I set up PUN2 and made join and create rooms menu and everything was working perfectly. But, when I tried implementing multiplayer into my game scene it did not work well. To simply put it, players need to click on red circles. Players see the same screen. There are white and red circles and if players click all red circles, all white circles get destroyed and more total circles spawn on random locations. In the script, I tried making it so master client controls spawning of circles. I added Photon view component to circle prefabs. I'm beginner in using PUN and this simple game is for me to learn it.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class CircleSpawner : MonoBehaviourPunCallbacks
    7. {
    8.     public GameObject circlePrefab;
    9.     public GameObject redCirclePrefab;
    10.     public GameObject explosionPrefab;
    11.     public GameObject redexplosionPrefab;
    12.  
    13.     public int totalCircles;
    14.  
    15.     private int currentCircles;
    16.  
    17.     private int currentredCircles;
    18.  
    19.     void Start()
    20.     {
    21.         GameObject[] objectsToDestroystart = GameObject.FindGameObjectsWithTag("Circle");
    22.  
    23.         for (int i = 0; i < objectsToDestroystart.Length; i++)
    24.         {
    25.             PhotonNetwork.Destroy(objectsToDestroystart[i]);
    26.         }
    27.  
    28.         if (PhotonNetwork.LocalPlayer.IsMasterClient)
    29.         {
    30.             totalCircles = 1;
    31.             currentCircles = 0;
    32.             currentredCircles = 0;
    33.             SpawnCircle();
    34.         }
    35.         else
    36.         {
    37.             return;
    38.         }
    39.  
    40.  
    41.     }
    42.  
    43.  
    44.     void SpawnCircle()
    45.     {
    46.         if (PhotonNetwork.LocalPlayer.IsMasterClient)
    47.         {
    48.             Vector3 randomPos = new Vector3(Random.Range(-9f, 9f), Random.Range(-5f, 5f), 0);
    49.             GameObject newCircle;
    50.             if (currentCircles % 3 == 0)
    51.             {
    52.                 newCircle = PhotonNetwork.Instantiate(redCirclePrefab.name, randomPos, Quaternion.identity);
    53.                 currentredCircles++;
    54.                 currentCircles++;
    55.             }
    56.             else
    57.             {
    58.                 newCircle = PhotonNetwork.Instantiate(circlePrefab.name, randomPos, Quaternion.identity);
    59.                 currentCircles++;
    60.             }
    61.         }
    62.         else
    63.         {
    64.             return;
    65.         }
    66.  
    67.      
    68.      
    69.     }
    70.  
    71.  
    72.     void Update()
    73.     {
    74.  
    75.         if (Input.touchCount > 0)
    76.         {
    77.  
    78.             Touch touch = Input.GetTouch(0);
    79.             Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
    80.             Collider2D hit = Physics2D.OverlapPoint(touchPos);
    81.  
    82.             if (hit != null && hit.CompareTag("Circle"))
    83.             {
    84.                 if (touch.phase == TouchPhase.Began)
    85.                 {
    86.                     PhotonNetwork.Destroy(hit.gameObject);
    87.                     if (PhotonNetwork.LocalPlayer.IsMasterClient)
    88.                     {
    89.                         currentredCircles--;
    90.                         currentCircles--;
    91.                     }
    92.                     Debug.Log("Clicked");
    93.                     PhotonNetwork.Instantiate(redexplosionPrefab.name, hit.transform.position, Quaternion.identity);
    94.                 }
    95.              
    96.             }
    97.  
    98.             if (hit != null && hit.CompareTag("WhiteCircle"))
    99.             {
    100.                 if (touch.phase == TouchPhase.Began)
    101.                 {
    102.                     PhotonNetwork.Destroy(hit.gameObject);
    103.                     if (PhotonNetwork.LocalPlayer.IsMasterClient)
    104.                     {
    105.                         currentCircles--;
    106.                     }
    107.                     Debug.Log("Clicked");
    108.                     PhotonNetwork.Instantiate(explosionPrefab.name, hit.transform.position, Quaternion.identity);
    109.                 }
    110.              
    111.             }
    112.  
    113.             if (currentredCircles == 0 && PhotonNetwork.LocalPlayer.IsMasterClient)
    114.             {
    115.                 totalCircles++;
    116.                 GameObject[] objectsToDestroy = GameObject.FindGameObjectsWithTag("WhiteCircle");
    117.                 for (int i = 0; i < objectsToDestroy.Length; i++)
    118.                 {
    119.                     if (PhotonNetwork.LocalPlayer.IsMasterClient)
    120.                     {
    121.                         PhotonNetwork.Destroy(objectsToDestroy[i]);
    122.                     }
    123.                 }
    124.                 for (int i = 0; i < totalCircles; i++)
    125.                 {
    126.                     if (PhotonNetwork.LocalPlayer.IsMasterClient)
    127.                     {
    128.                         SpawnCircle();
    129.                     }
    130.                     else
    131.                     {
    132.                         return;
    133.                     }
    134.                 }
    135.             }
    136.         }
    137.     }
    138. }
    There are some very weird ways I tried to fix the issue with. The problem is that on other client that is not the master, on the start 8 extra red circles spawn, and 1 red circle that is visible on the master (there should be 1 circle at the start). After that, everything works fine but red circles are still on other client's screen.