Search Unity

[SOLVED] How can I initialize a variable A, with the value of variable B. In a certain way....

Discussion in 'Scripting' started by Etaodev, Dec 19, 2019.

  1. Etaodev

    Etaodev

    Joined:
    Dec 8, 2019
    Posts:
    13
    Hello,

    The thread title limit isn't enough to capture the whole of my question. So, here is the full question/problem.
    "How can I initialize a variable A, with the value of variable B. In a way where when I change variable A, it doesn't change the value of variable B. Essentially, how can I pass the information ONLY, ultimately protecting variable B."

    So right now. There is two scripts, variable B is a static variable from script_one. And variable A is a yet to be declared variable in script_two. And I want to decrease the value of variable a by 1 in the update function.

    Script One:
    Code (CSharp):
    1. public class script_one : MonoBehaviour
    2. {
    3.     static public int variable_b = 10;
    4. }

    Script Two:

    Code (CSharp):
    1. public class script_two : MonoBehaviour
    2. {
    3.     //Yet to be declared variable. Wants only the information of
    4.     //variable b
    5.     private void Update(){
    6.           //variable_a -= 1;
    7.     }
    8. }

    If I were to set variable a as variable b like this:
    Code (CSharp):
    1. public class script_two : MonoBehaviour
    2. {
    3.     public int variable_a;
    4.     private void Start(){
    5.     variable_a = script_one.variable_b;
    6.     }
    7.     private void Update(){
    8.           //variable_a -= 1;
    9.     }
    The value of variable b is also going to be changed. '

    So, how can I only pass the value of variable b into variable a. So that later when I alter variable a, the value of variable b will not be changed.

    Please help me ;-;
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    828
    Incorrect. The value of
    variable_b
    will be copied into
    variable_a
    , and any modifications you make to
    variable_a
    will have no effect on
    variable_b
    .

    In C# there are two ways that things can get passed around. By value or by reference. Generally speaking, objects (classes, arrays, etc.) are passed by reference. Everything else (numbers, booleans, structs, enums, etc.) are passed by value. There are some circumstances in which you can pass primitives by reference, but those situations are outside the scope of this post.

    In your example, both
    variable_a
    and
    variable_b
    are integers, which are primitive and therefore are passed by value. When you call
    variable_a = variable_b
    , you are only setting
    variable_a
    's held value to be equal to
    variable_b
    's. You are not setting
    variable_a
    to be a reference to
    variable_b
    .

    In other words, there is no problem. You are already doing the right thing.
     
  3. Etaodev

    Etaodev

    Joined:
    Dec 8, 2019
    Posts:
    13
    I see. So I have been overthinking all this time... Thank you so much for you reply. Have a good day!
     
unityunity