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 Updating values on one object update the values on all objects

Discussion in 'Scripting' started by ranjansikand, Jul 14, 2023.

  1. ranjansikand

    ranjansikand

    Joined:
    Apr 8, 2021
    Posts:
    103
    I'm making a game where you control a team of soldiers. Each soldier is a script object that contains data about it.

    Code (CSharp):
    1. public class Soldier {
    2.   public int maxHealth;
    3.   public int currentHealth;
    4.   public int power;
    5. }
    Everything works fine if I have a team of several different units. However, if my team is made up of multiples of the same unit, I run into an issue. When you update a value, it updates it for all instances of that unit.

    Are there any steps I can go through to troubleshoot this issue?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    We're going to need to see more of your setup. But, assuming you have say... 3 soldiers and each soldier has a "SolderStat" class for example which had a Soldier variable on it, changing the value of maxHealth of one of those Soldier variables will not effect the other based on what little info you've shown.

    As for troubleshooting, you need to look at what you are trying to modify. Where are your instances of Soldier at? What are you actually targeting when you change a value on Soldier.
     
  3. ranjansikand

    ranjansikand

    Joined:
    Apr 8, 2021
    Posts:
    103
    The instances are held in an array, and assigned as such:
    Code (CSharp):
    1. public static bool AddUnit(Soldier soldier = null, int opening = -1) {
    2.         // If no index is passed, find open slot
    3.         if (opening == -1) {
    4.             for (int i = 0; i < Party.Length; i++) {
    5.                 if (Party[i] == null) {
    6.                     opening = i;
    7.                     break;
    8.                 }
    9.             }
    10.  
    11.             if (soldier == null) {
    12.                 return opening != -1;  // Check and return if space is available
    13.             }
    14.         }
    15.      
    16.         // If index is passed or space is open
    17.         Party[opening] = soldier;
    18.         return true;
    19.     }
    I was trying to have the function serve two purposes, but this is the basic methodology. I then access them by index. Space 0 is the one that is most frequently accessed, as it is the lead in battle. However, changing the health of space 0 changes the health of the rest of the same-type units in the party.
     
  4. ranjansikand

    ranjansikand

    Joined:
    Apr 8, 2021
    Posts:
    103
    Never mind, I realized the issue. I'm creating the objects before assigning them, so I'm literally assigning the same object to multiple slots...
     
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Seems the easiest way to help people is to wait 30 mins for them to figure it out for themselves!..
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Sometimes a nudge or a second pair of eyes helps get a person back on the right track. It happens to a lot of developers, especially if you've been typing code all day long.
     
    ranjansikand likes this.