Answer the question
In order to leave comments, you need to log in
When a variable is passed to a function, it changes. How to make sure this doesn't happen?
When a variable is passed to a function, it changes. Here is the code:
void Start()
{
int[] pos = new int[2];
pos[0] = 1;
pos[1] = 2;
ChangePos(pos);
Debug.Log(pos[0] + " " + pos[1]); // вывод 3 4
}
void ChangePos(int[] value_to_change)
{
value_to_change[0] = 3;
value_to_change[1] = 4;
}
Answer the question
In order to leave comments, you need to log in
Something like this. Why did you need to change?
void ChangePos(int[] value_to_change)
{
var nn = new int[value_to_change.Length];
value_to_change.CopyTo(nn,0);
nn[0] = 3;
nn[1] = 4;
}
How to make sure that when passing the value of a variable to a function and changing it in the function, its value does not change?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question