Search Unity

Question Prefab component properties are not changing at runtime through a script

Discussion in 'Prefabs' started by Rashmika98, Oct 16, 2022.

  1. Rashmika98

    Rashmika98

    Joined:
    Sep 24, 2022
    Posts:
    9
    Hi, I have prefab(NetworkRunner one) which has a Recorder(photon voice) component in it. I need to change one of its bool property called, recorder.TransmitEnabled through a toggle button. I created a script to do that functionality which displayed in below.
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEditor;
    6. using UnityEngine.UI;
    7. using Fusion;
    8. using Photon.Voice.Unity;
    9.  
    10. [RequireComponent(typeof(Toggle))]
    11. public class MicToggle : MonoBehaviour
    12. {
    13.     [SerializeField]
    14.     Recorder recorder;
    15.     Toggle micToggle;
    16.  
    17.     public NetworkRunner prefab;
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         recorder = prefab.GetComponent<Recorder>();
    23.         micToggle = GetComponent<Toggle>();
    24.         if (!recorder.TransmitEnabled)
    25.         {
    26.             Debug.Log("disabled");
    27.             micToggle.isOn = false;
    28.         }
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.  
    35.     }
    36.  
    37.     public void ToggleMicOnValueChange(bool state)
    38.     {
    39.         if (state)
    40.         {
    41.             recorder.TransmitEnabled = true;
    42.         }
    43.         else
    44.         {
    45.             recorder.TransmitEnabled = false;
    46.         }
    47.     }
    48. }
    49.  
    However when I debugged the values of TransmitEnabled property, it shows that the value is changed but I it is not visible at runtime in inspector(when toggling the button recorder.TransmitEnabled is not changing.) But after running the code the last toggled value is saved in the prefab property. I need to observe the property change through the inspector when toggling the button at runtime. Any idea why this is happening?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,882
    Have you looked into the code for recorder.TransmitEnabled property? It could have some logic that prevents it from changing state. It could also be a property for a backing field and that backing field is what you see in the Inspector, but not the property.

    How is ToggleMicOnValueChange hooked up in the GUI? I see it takes a bool as input which is uncommon for a toggle, at least for me.

    I'm used to using a Toggle event like this:
    Code (CSharp):
    1. public void ToggleMicOnValueChange() => recorder.TransmitEnabled = micToggle.isOn;
    Or if you want to pass in the Toggle from the GUI (that works with any toggle but requires dropping the Toggle object on the GUI event):
    Code (CSharp):
    1. public void ToggleMicOnValueChange(Toggle toggle) => recorder.TransmitEnabled = toggle.isOn;
    Lastly, just a note: remove the Update method. It does nothing except getting called, adding some overhead to every instance of the script.
     
  3. Rashmika98

    Rashmika98

    Joined:
    Sep 24, 2022
    Posts:
    9
    Hi,
    Thank you for your valuable answer and comments:). I was able to solve that issue.