Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Stardog · May 21, 2011 at 02:13 PM · arraylistarrayslistsundoback

Undo/back system using a List/Array

In my game the player can only move between MoveNodes. On colliding with the node, it adds the node name to a List. So as I travel around I amass a large list of previously visited MoveNodes.

On certain nodes a "back" button appears, which should send the player back to the 2nd most recent MoveNode in the List. How do I do this? Do I get the Length - 1 or something?

Also, can I clear anything but the most recent 5 objects in the list so it doesn't get so big?

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
Best Answer

Answer by GesterX · May 21, 2011 at 02:55 PM

Something like this (i'm assuming your array is called nodes):

 var targetNodeIndex : int;
 var targetNode : GameObject;
 
 //the function called by your back button
 function GoBack()
 {
   targetNodeIndex = nodes.length - 2;
   //retrieves the second to last node. Eg in a 5 node array - this returns node 4.
   targetNode = nodes[targetNodeIndex];
 }

We use "length-2" because nodes.length will return the length of the array BUT in a 5 node length array the elements will be in indexes 0,1,2,3, and 4. So to get at the second to last node we need to access the index that is 2 less than the length.

Once you have retrieved your node just set the players transform to the same as the node trnasform.

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 Stardog · May 21, 2011 at 04:08 PM 0
Share

Thanks. $$anonymous$$y back button runs this code now:

allPreviousLocations[0].transform.position

I'll work on improving the functionality with those variables.

avatar image
1

Answer by Edy · May 21, 2011 at 02:54 PM

You can create a List of MoveNodes, then use the List functions Add, Remove, etc. and its property Item for managing and accessing the items as you want. You can see an example here:

http://forum.unity3d.com/threads/79760-How-to-use-generics-in-unity-javascript?p=510611#post510611

The reference for the List methods and properties is here:

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

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 Stardog · May 21, 2011 at 04:06 PM 0
Share

Thanks, this let me limit the size: if list.Count > 2 list.RemoveRange(0, 1)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to return index of List element? 1 Answer

How to modify array values? 1 Answer

Is there a way to remove array entries in the editor? 4 Answers

How can you do calculations on two lists? 1 Answer

Problem with arrays in a list 1 Answer


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