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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Get component's property

Discussion in 'Scripting' started by Alvarezmd90, Feb 24, 2020.

  1. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    149
    Hey forum
    I'm trying to avoid at all costs to use get component on fixed update.
    However, I want to alter values of other objects relative to the sun's light.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class RelativeToSun : MonoBehaviour {
    4.     [SerializeField] private string Component="Light";
    5.     private Component ComponentToUse;
    6.     private Light Sun;
    7.     public bool OnUpdate;
    8.     public float Multiplier=1;
    9.     public bool Invert = false;
    10.     private void Awake()
    11.     {
    12.         Sun = DayAndNightCycle.Sun.gameObject.GetComponent<Light>();
    13.         switch (Component)
    14.         {
    15.             case "Light":
    16.                 ComponentToUse = GetComponent<Light>();
    17.                 break;
    18.             case "Sound":
    19.                 ComponentToUse = GetComponent<AudioSource>();
    20.                 break;
    21.             case "Flare":
    22.                 ComponentToUse = GetComponent<LensFlare>();
    23.                 break;
    24.         }
    25.         SetValue();
    26.     }
    27.     private void FixedUpdate () {
    28.         if (!OnUpdate) return;
    29.         SetValue();
    30.     }
    31. }
    Code (CSharp):
    1.  private void SetValue()
    2.     {
    3.         var TheValue = Sun.intensity * Multiplier;
    4.         if (Invert)
    5.         {
    6.             TheValue = (1-Sun.intensity) * Multiplier;
    7.         }
    8.         switch (Component)
    9.         {
    10.             case "Light":
    11.                 GetComponent<Light>().intensity = TheValue;
    12.                 break;
    13.             case "Sound":
    14.                 GetComponent<AudioSource>().volume = TheValue;
    15.                 break;
    16.             case "Flare":
    17.                 GetComponent<LensFlare>().color = GetComponent<Light>().color;
    18.                 break;
    19.         }
    20.     }
    As you may see, I use this code for lights, sound and flares. However, when I'm trying to get component on awake (which worked), I cannot get their properties such as intensity, volume and color to be accessed from Component to use.. Does anyone know how to do this?
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    Alvarezmd90 likes this.
  3. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    149
    Woah it works! Thanks man. :eek:

    Code (CSharp):
    1.     private void SetValue()
    2.     {
    3.         var TheValue = Sun.intensity * Multiplier;
    4.         if (Invert)
    5.         {
    6.             TheValue = (1-Sun.intensity) * Multiplier;
    7.         }
    8.         switch (Component)
    9.         {
    10.             case "Light":
    11.                 ((Light)ComponentToUse).intensity = TheValue;
    12.                 break;
    13.             case "Sound":
    14.                 ((AudioSource)ComponentToUse).volume = TheValue;
    15.                 break;
    16.             case "Flare":
    17.                 ((LensFlare)ComponentToUse).color = Sun.color;
    18.                 break;
    19.         }
    20.     }