Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Reference private variables without making it public

Discussion in 'Scripting' started by fransiscodelacalle121, May 21, 2023.

  1. fransiscodelacalle121

    fransiscodelacalle121

    Joined:
    Jun 5, 2021
    Posts:
    2
    Google translate, so sorry if something is not understood.

    Hello, does anyone know if it is possible to reference a variable from another script that is private without making it public?

    Perhaps another way of explaining myself is by saying that currently I would like to create a script B linked to script A, since script A has become very large and it has many variables that I would not want to make public, is there a way to link two scripts so that they have the same variables? variable?? An answer would really help me a lot, thank you very much.
     
  2. tsukimi

    tsukimi

    Joined:
    Dec 10, 2014
    Posts:
    85
    It depends on how strong you want it private, if you just want other class to have read access but not write access, you can do this:
    Code (CSharp):
    1.  
    2. public class A
    3. {
    4.     private int someInt;
    5.     public int SomeInt => someInt; // getter only property
    6.     // or
    7.     public int SomeInt { get; private set; } // private setter property
    8. }
    9.  
    if you want to keep A's variable totally secret, thant a nested class technique could also be used:
    Code (CSharp):
    1.  
    2. public class A
    3. {
    4.     private int someInt;
    5.  
    6.     public class B
    7.     {  // if you want to separate files, you can define A as partial
    8.         private A a;
    9.         public B(A a)
    10.         {
    11.             this.a = a;
    12.         }
    13.         private void AddToASomeInt()
    14.         {
    15.             // nested class can access parent class's private field
    16.             a.someInt += 1;
    17.         }
    18.     }
    19. }
    If you want totally same variable, than you can create a data container class, and let A and B have the same instance.
    Code (CSharp):
    1.  
    2. public class DataContainer
    3. {
    4.     public int SomeInt;
    5. }
    6.  
    7. // if both class received same DataContainer instance, they share same variables.
    8. // DataContainer's field is public, but A and B holds it has private, so you can't access DataContainer by A or B
    9. // (except who gives DataContainer to A and B)
    10. public class A
    11. {
    12.     private DataContainer dataContainer;
    13.     public void SetDataContainer(DataContainer d)
    14.     {
    15.         dataContainer = d;
    16.     }
    17. }
    18.  
    19. public class B
    20. {
    21.     private DataContainer dataContainer;
    22.     public void SetDataContainer(DataContainer d)
    23.     {
    24.         dataContainer = d;
    25.     }
    26. }
    27.  
     
    fransiscodelacalle121 and _geo__ like this.
  3. Draad

    Draad

    Joined:
    Feb 17, 2011
    Posts:
    325
    Hello,

    One of the way to achieve that is to use an Accessor. You can read more about it here.
    There is a trick, if you define an accessor without defining a set, you create a ReadOnly accessor. That mean any other script can read the value but cannot modify it.

    Code (CSharp):
    1.  
    2. int age = 23;
    3.  
    4. // Accessor
    5. int Age
    6. {
    7.    get { return age; }
    8. }
    9.  
    There is others solutions, more complex, like inheritance, depending on your specific need.
     
    fransiscodelacalle121 likes this.
  4. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,393
    I think the partial keyword is just what you're looking for.

    Player.Variables.cs:
    Code (CSharp):
    1. public partial class Player : MonoBehaviour
    2. {
    3.     private float speed;
    4. }
    Player.Methods.cs:
    Code (CSharp):
    1. public partial class Player
    2. {
    3.     private void Update() => transform.Translate(transform.forward * speed);
    4. }
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,318
    Definitely sounds like you want partial (explained above).
     
    fransiscodelacalle121 likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,761
    I would defer to the above but if it's just raw access you need in your own assembly (for you) then make the accessibility modifier(s) internal rather than private.

    Really does depend on who you are guarding access from whether that be yourself or others using it.
     
  7. fransiscodelacalle121

    fransiscodelacalle121

    Joined:
    Jun 5, 2021
    Posts:
    2
    thank you very much, it worked perfectly
     
    MelvMay, orionsyndrome and SisusCo like this.