Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tomeu90 · Sep 26, 2020 at 09:40 PM · coroutinevariablesvalues

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tomeu90 · Sep 27, 2020 at 06:35 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

146 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges