Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. 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.

Resolved Disabling component works but also doesn't at the same time

Discussion in 'Multiplayer' started by jlorenzi, May 21, 2023.

  1. jlorenzi


    May 2, 2021
    So this is a confusing issue. I have a script for each class in my game. When a player selects a new class all the scripts for the other classes get unenabled and then the one they select gets enabled.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class SelectElement : MonoBehaviour
    6. {
    7.     public enum Element {fire, earth, water};
    8.     public Element element = new Element();
    9.     public Firebending fire;
    10.     public Earthbending earth;
    11.     public Waterbending water;
    12.     public RoundManager roundManager;
    14.     void OnTriggerEnter(Collider other)
    15.     {
    16.         if ( other.CompareTag("Player") )
    17.         {
    18.             Debug.Log("selected element"); // works as expected
    19.             earth.enabled = false;
    20.             fire.enabled = false;
    21.             water.enabled = false;
    22.             switch ( element )
    23.             {
    24.                 case
    25.                     fire.enabled = true;
    26.                     break;
    27.                 case
    28.                     earth.enabled = true;
    29.                     break;
    30.                 case Element.water:
    31.                     water.enabled = true;
    32.                     break;
    33.             }
    35.             roundManager.enabled = true;
    36.         }
    37.     }
    38. }
    but even though this code seems to work (e.g. in the inspector the only script that's enabled is the one that the player selected), the player can still use all the skills from the classes that used to be enabled.

    If I made that script run on the server-side wouldn't it select every players class? How can I fix this issue?

    I'm using Photon PUN by the way.
  2. tobiass


    Apr 7, 2009
    PUN does not really have a "server side". Collisions are running locally on each client and unless you manually sync the selected class for some player, this is only a local state variable.

    You probably want to set the class as some Custom Player Property. That gets synced to everyone in the room and can be used to enable / disable scripts.