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

how to calculate the largest value of a variable in multiple scripts

Discussion in 'Editor & General Support' started by KonniosGames, Jul 6, 2021.

  1. KonniosGames

    KonniosGames

    Joined:
    Dec 22, 2018
    Posts:
    18
    ok, I have 2 scripts. the one is managing the game (GameManager script) and the other is keeping some player data (PlayerData script). The (PlayerData script) is used by 4 gameObjects and has a variable (int RandomNumber) which has a different value in each gameObject. I want the (GameManager script) to calculate which of the (PlayerData scripts) attached in each gameObject has the largest value of the variable (RandomNumber) and then return information to the (PlayerData script) that contains that value.

    Can you help me please?
     
  2. Pykrete

    Pykrete

    Joined:
    Jan 8, 2018
    Posts:
    9
    Sounds like you need an array of PlayerData in your GameManager script that stores a reference to each PlayerData that's active in the game so that you can iterate through the array looking for the one that's largest.
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Exactly what @Pykrete said above, but also if you don't want to pre-drag those references around, you could just use FindObjectsOfType<PlayerData>() and get them all at once, ASSUMING that all of them you want are in the scene and there are none that you DON'T want.

    I would not recommend doing this every frame however, but doing it once (and storing the result) after all the players spawn in would be fine, or perhaps just before you pop up a UI to list "Who is the strongest" for instance.

    Just be sure to handle zero objects being present, or the object list changing over time, such as if a player dies off or is added.
     
  4. KonniosGames

    KonniosGames

    Joined:
    Dec 22, 2018
    Posts:
    18
    Ok I stored all of my scripts in the array. Now, do you know how I can access the variaable in each script and calculate the largest value?
     
  5. Pykrete

    Pykrete

    Joined:
    Jan 8, 2018
    Posts:
    9
    I don't want to sound rude, but it sounds like it may be helpful if you were to work through some beginner scripting tutorials, because questions like how to access a variable in script A from inside script B are fairly fundamental things that a programmer needs to know. Thankfully, Unity has a series of videos designed for exactly this purpose:

    https://learn.unity.com/project/beginner-gameplay-scripting

    For calculating the largest value in an array, there are several potential methods you could use, but I would recommend learning about the array sorting methods that are built in to C#. You will probably find that sorting values is a pretty common task as you develop more of your game, so it's a useful thing to learn.
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    This is a super-basic data processing need.

    The steps are to iterate the array and ask if any item is larger, and make that the max.

    ASSUMES integer for simplicity here... see below.

    Code (csharp):
    1. int max = 0;       // largest quantity
    2. int which = 0;              // who has the largest quantity
    3.  
    4. for (int i = 0; i < MyArray.Length; i++)
    5. {
    6.   // take the value out for consideration
    7.   var value = MyArray[i].TheValue;
    8.  
    9.   if (i == 0)   // first one is by default the biggest
    10.   {
    11.     max = value;
    12.     which = 0;
    13.   }
    14.  
    15.   if (value > max)   // consider each one to see if it is bigger
    16.   {
    17.     max = value;
    18.     which = i;
    19.   }
    20. }
    21.  
    22. // now max has the biggest value and which is the item index that it was in
    23.  
    Millions of items on the net with other much more flexible and type-adaptable approaches, some built in... here's one:

    https://stackoverflow.com/questions/1034282/find-highest-integer-in-a-generic-list-using-c
     
    KonniosGames likes this.
  7. KonniosGames

    KonniosGames

    Joined:
    Dec 22, 2018
    Posts:
    18
    Here's the problem. I can't calculate the largest value in my array because my array consists of scripts and not numbers. Every script has a variable which has a different value in each script and I want to calculate which of the scripts contains the variable with the greatest value. The reason my array consists of scripts and not just the values of the variables, is beacuse I want to return some information to the script that has the variable with the gratest value.
     
  8. SequentialZ

    SequentialZ

    Joined:
    Oct 3, 2020
    Posts:
    3
    If you want to return some information to the PlayerData script which has the largest value, simply have a PlayerData variable that you update when a new larger value is found.
    (Updating Kurt-Deckker's script)

    Code (CSharp):
    1.  
    2. PlayerData largestPlayerData; // each time a value is larger, set reference to that PlayerData
    3. int max = 0;       // largest quantity
    4. int which = 0;              // who has the largest quantity
    5. for (int i = 0; i < MyArray.Length; i++)
    6. {
    7.   // take the value out for consideration
    8.   int value = MyArray[i].TheValue;
    9.   if (i == 0)   // first one is by default the biggest
    10.   {
    11.     max = value;
    12.     largestPlayerData = MyArray[i];
    13.     which = 0;
    14.   }
    15.   if (value > max)   // consider each one to see if it is bigger
    16.   {
    17.     max = value;
    18.     largestPlayerData = MyArray[i]; // Now you have the largest value's PlayerData
    19.     which = i;
    20.   }
    21. }
    22. return largestPlayerData; // PlayerData that has the largest value is returned
    23.  
     
    Kurt-Dekker likes this.
  9. KonniosGames

    KonniosGames

    Joined:
    Dec 22, 2018
    Posts:
    18
    ok done! thank you so much :)