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

Question Change multiplayer color light

Discussion in 'Scripting' started by brano42, Jun 25, 2023.

  1. brano42

    brano42

    Joined:
    Aug 25, 2020
    Posts:
    77
    Can someone help me with a script that changes the color of the light?

    here is script
    Code (CSharp):
    1. using Photon.Pun;
    2. using System;
    3. using UnityEngine;
    4. using UnityEngine.Experimental.GlobalIllumination;
    5.  
    6. namespace UnityStandardAssets.Vehicles.Car
    7. {
    8.     public class BrakeLight : MonoBehaviour
    9.     {
    10.         public CarController car;
    11.  
    12.         public GameObject[] pointLight;
    13.         public PhotonView PV;
    14.  
    15.         public float stop;
    16.         private void Start()
    17.         {
    18.          
    19.         }
    20.  
    21.  
    22.         private void Update()
    23.         {
    24.             stop = this.car.BrakeInput;
    25.             if (PV.IsMine)
    26.             {
    27.                 PV.RPC("ChangeLight", RpcTarget.All, stop, (object)pointLight);
    28.             }
    29.        
    30.              
    31.         }
    32.  
    33.         [PunRPC]
    34.       private  void ChangeLight( float stop , GameObject[] pointLight  )
    35.         {
    36.             stop = this.car.BrakeInput;
    37.  
    38.             if (stop > 0f)
    39.             {
    40.                pointLight[0].GetComponent<Light>().color = Color.white;
    41.                pointLight[1].GetComponent<Light>().color = Color.white;
    42.             }
    43.             if (stop == 0f)
    44.             {
    45.                pointLight[0].GetComponent<Light>().color = Color.red;
    46.                pointLight[1].GetComponent<Light>().color = Color.red;
    47.             }
    48.         }
    49.     }
    50.  
    51. }
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    We can only help if you tell us what the issue is
     
    Yoreki likes this.
  3. brano42

    brano42

    Joined:
    Aug 25, 2020
    Posts:
    77
    it doesn't work at all even if I put a debug log a similar script worked well
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    Do you mean a debug log worked fine?
    Then log the stop float, all entries in the array, the getcomponents, etc to see where it exactly stops running
     
  5. brano42

    brano42

    Joined:
    Aug 25, 2020
    Posts:
    77
    it works thanks for your help
     
  6. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    Do share the fix :p
     
  7. brano42

    brano42

    Joined:
    Aug 25, 2020
    Posts:
    77
    here is the fix script

    Code (CSharp):
    1. using Photon.Pun;
    2. using System;
    3. using UnityEngine;
    4. using UnityEngine.Experimental.GlobalIllumination;
    5.  
    6. namespace UnityStandardAssets.Vehicles.Car
    7. {
    8.     public class BrakeLight : MonoBehaviour
    9.     {
    10.         public CarController car;
    11.  
    12.         public GameObject[] pointLight;
    13.         public PhotonView PV;
    14.  
    15.         public float stop;
    16.         private void Start()
    17.         {
    18.  
    19.         }
    20.  
    21.  
    22.         private void Update()
    23.         {
    24.            
    25.             if (PV.IsMine)
    26.             {
    27.                 stop = this.car.BrakeInput;
    28.                 PV.RPC("ChangeLight", RpcTarget.All, stop);
    29.             }
    30.  
    31.  
    32.         }
    33.  
    34.         [PunRPC]
    35.         private void ChangeLight(float stop)
    36.         {
    37.          
    38.             Debug.Log("color Work");
    39.             if (stop > 0f)
    40.             {
    41.                 pointLight[0].GetComponent<Light>().color = Color.white;
    42.                 pointLight[1].GetComponent<Light>().color = Color.white;
    43.                 Debug.Log("color White");
    44.             }
    45.             if (stop == 0f)
    46.             {
    47.                 pointLight[0].GetComponent<Light>().color = Color.red;
    48.                 pointLight[1].GetComponent<Light>().color = Color.red;
    49.                 Debug.Log("color Red");
    50.             }
    51.         }
    52.     }
    53.  
    54. }
    55.  
     
    DevDunk likes this.