- Home /
Keep different values of the same variable
Hello everyone,
I'm a begginer with programming and Unity. My question is:
Is it possible to store different values of the same variable? For.e.
I've got values for "rotation.x" and "rotation.y" that are constantly changing. Can I store that values in a moment of time inside variables? So I can compare/operate with other values of the same variables in the future. How?
what I try to achieve is: when the difference between rotation1.x and rotation2.x is positive/negative, change the position of an object to one side or another.
I tried with coroutines, but maybe im doing something wrong.
Sorry if im not very clear.
Thank you very much.
Answer by N-8-D-e-v · Sep 26, 2020 at 11:34 PM
There is no reason why you can't, just make them member variables. Say you want to have three "presets" for the player position, just do this
Vector3 pos1 = whatever you want;
Vector3 pos2 = whatever you want;
Vector3 pos3 = whatever you want;
void SetToPos(Vector3 pos)
{
transform.position = pos;
}
//to set pos to pos1
SetToPos(pos1);
Answer by tomicz · Sep 26, 2020 at 11:39 PM
Yes, this is absolutely doable and it's very common to use in programming.
The thing that you need is Lists, and you can store multiple values of the same data type.
This is how you initialize the list
List<float> tempRotationsList = new List<float>();
Then you store data to a list by doing this
tempRotationsList.Add(rotation.x); or tempRotationList.Add(rotation.y);
And you do this for every data type, just the value and type have to match. So you cannot add integers to the list of strings, but you can always convert them.
Thank you,
Im trying to move a camera in a 2D environment depending on the rotation of the device (gyroscope), like a flat panorama viewer. As it is a 2D scene I can't rotate it, so my idea is to create an empty object that follows the gyroscope rotation, and depending of the different values of the object rotation in time (if the difference is positive/negative) move the camera in a specific direction. I don't know if it's the best way to achieve it, i ve tried it in different ways and i can't get results.
Your answer
Follow this Question
Related Questions
Scripting problem, with values and input.getbutton 1 Answer
Button on one scene changes variable on next scene 1 Answer
Script works with hard coded values but not variables 2 Answers
How to change a value of a reference object variable after a while ? 0 Answers
How to change the values of an animation using a variable ??? 2 Answers