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 can you track different floats from another script?

Discussion in 'Scripting' started by rende36, Aug 3, 2023.

  1. rende36

    rende36

    Joined:
    May 31, 2023
    Posts:
    1
    I have a system that can graph data provided I can track it, the issue is I currently have a different script for each different variable that needs graphing. Is there a way to point (for lack of a better word) at any desired, and public, float so that I can combine all these scripts into 1? I understand ideally I'd have it update through things like setters and getters, but most of the time I need values from rigidbodies, and without writing a script that pushes the values to the tracker (which seems as poor of practice as having a hard-coded tracker) is there a way I can tell it the component 'track this variable'?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    I feel a little confused as to what you want, but if I understand correctly... Can you get a value off of a component with another component? Yes. That would just be using GetComponent. Now, if you want to track changes and have something update when a value is changed, you would probably want to use Events and trigger the event when a value changes. So, if script 1 is drawing a graph and script 2 has the value. Then script 1 would be subscribed to an event on script 2. When the value on script 2 changes, you call the event and script 1 can update the graph or whatever it needs to do.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I usually wrap these up and pass them in as a delegate.

    My JetblastFeeler class uses this...

    https://pastebin.com/5mAPGH1d

    it's kind of a wonky mess of a class. You make it with:

    Code (csharp):
    1.         JetblastNozzle = new GameObject( "JetblastNozzle").transform;
    2.         JetblastNozzle.SetParent( player.transform);
    3.         JetblastNozzle.localRotation = Quaternion.Euler( 90, 0, 0);
    4.         JetblastNozzle.localPosition = Vector3.zero;        // driven elsewhere depending on camera
    5.         JetblastFeeler.Attach( nozzle: JetblastNozzle,
    6.             GetAmount: () =>
    7.             {
    8.                 float amount = 30.0f * indicatedPower;
    9.                 return amount;
    10.             },
    11.             GetDistance: () =>
    12.             {
    13.                 return 8.0f;
    14.             }
    15.         );
    so those two delegates are used to decide how to operate.

    You need to make the GameObject to pass in (I did this because some games attach to existing stuff in a prefab). The .localPosition is your own local positioning for relative jet nozzle position on player.
     
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    A structure I use, which may not work for your case, but could help just knowing it exists:

    Code (CSharp):
    1. public class Master : MonoBehavior
    2. {
    3.     public static List<Data> waterData = new List<Data>();
    4.     public static List<Data> landData = new List<Data>();
    5.     public static List<Data> hillData = new List<Data>();
    6.    
    7.     public List<float> GetWaterData()
    8.     {
    9.         List<float> tempList = new List<float>();
    10.         for (int i = 0; i < waterData.Count; i++)
    11.         {
    12.             tempList[i].Add(waterData[i].dataZ);
    13.         }
    14.         return tempList;
    15.     }
    16. }
    Code (CSharp):
    1. public class Data : Master
    2. {
    3.     public int dataX;
    4.     public float dataZ;
    5.     public string dataName;
    6.     public Rigidbody rig;
    7.    
    8.     public void SetGeneralComponents()
    9.     {
    10.         rig = GetComponent<Rigidbody>();
    11.     }
    12. }
    Code (CSharp):
    1. public class Water : Data
    2. {
    3.     void Awake()
    4.     {
    5.         waterData.Add(this);
    6.         dataX = 5;
    7.         dataZ = transform.position.z;
    8.         dataName = "WaterUnderTheBridge";
    9.         SetGeneralComponents();
    10.     }
    11. }
    12. // same for other data types, etc...
    Code (CSharp):
    1. public class PieChart : Master
    2. {
    3.     public List<float> waterDataPoints = new List<float>();
    4.    
    5.     public void UpdateWaterData()
    6.     {
    7.         waterDataPoints = GetWaterData();
    8.         // other code if needed...
    9.     }
    10.    
    11.     void Update()
    12.     {
    13.         if (showData)
    14.         {
    15.             // pie chart code via index
    16.             // or foreach waterDataPoints
    17.             // etc...
    18.         }
    19.     }
    20. }
    Which I'm sure could be cleaned up a bit, or made even more sleek. But I've found value in using Inheritance and static calls to classes(like singletons). Which makes it very easy to say "that script over there, let me get any of your variables" in a nut shell.

    But I'm sure as mentioned, there are better(or cleaner) methods. I figure I'd give a different opinion. :cool:
     
  5. SeerSucker69

    SeerSucker69

    Joined:
    Mar 6, 2021
    Posts:
    65
    You could have "global variables" in a singleton, have all your scripts refer to them (read and write them)

    Google up "singleton"

    Singletons
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Kind of just sounds like these different values should express a common interface. Then you can just gather tham all up via said interface and read from them as necessary.