Search Unity

How to get a class function by valor.

Discussion in 'Scripting' started by rubcc95, Feb 1, 2020.

  1. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Hello all and ty for your time :) I'm pretty newbie at programming and ive an issue i dont know how to fix:

    Ive this class:

    Code (CSharp):
    1. public class WeaponController : MonoBehaviour
    2. {
    3.  
    4.     public int[] stats = new int[8];
    5.  
    6.     public void SetStats(int[] value)
    7.     {
    8.         for (int i = 0; i <= 7; i++)
    9.         {
    10.             stats[i] = value[i];
    11.          
    12.         }
    13.     }
    14.  
    15.     public int[] GetStats()
    16.     {
    17.         return stats;
    18.     }
    19. }
    20.  
    And with another class im trying to swap 2 of this classes int[] stats at this code:
    Code (CSharp):
    1. //We take both WeaponControllers from different objects.
    2. WeaponController newWeaponStats = collision.gameObject.GetComponent<WeaponController>();
    3. WeaponController weaponStats = GetComponentInChildren<WeaponController>();
    4.  
    5. //We made a new variable allocating one of this arrays values from the WeaponController class
    6. Int[] temporalStats = newWeaponStats.GetStats();
    7.  
    8. //We swap the values with this 2 operations
    9. newWeaponStats.SetStats(weaponStats.GetStats());
    10. weaponStats.SetStats(temporalStats);
    The issue beings when temporalStats values are modified when i do
    Code (csharp):
    1. newWeaponStats.SetStats(weaponStats.GetStats());
    because I think I'm taking at temporalStats the reference from this values. How could I take the values themselves intead of their ref? As we can make with the ByVal keyword at VB.

    Thanks you so much!
     
    Last edited: Feb 1, 2020