How can I move item from end of list to the start?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveObjects : MonoBehaviour
{
public float speed = 3f;
private GameObject objectstoMove;
private List<GameObject> objectsMoving = new List<GameObject>();
private float distanceTravelled = 0;
private Vector3 lastPosition;
// Use this for initialization
public void Init()
{
objectstoMove = GameObject.FindGameObjectsWithTag("Test");
objectsMoving = new List<GameObject>(objectstoMove);
lastPosition = objectstoMove[objectstoMove.Length].transform.position;
}
// Update is called once per frame
void Update()
{
if (objectstoMove != null)
{
float step = speed * Time.deltaTime;
for (int i = 0; i < objectstoMove.Length; i++)
{
if(distanceTravelled >= 50.0f)
{
objectsMoving.Remove(objectsMoving[objectsMoving.Count]);
}
objectsMoving[i].transform.Translate((objectsMoving[i].transform.up + objectsMoving[i].transform.forward) * step);
distanceTravelled += Vector3.Distance(objectsMoving[objectsMoving.Count].transform.position, lastPosition);
lastPosition = objectsMoving[objectsMoving.Count].transform.position;
}
}
}
}
In this part I want to take the last object in the list and move it to the start of the list:
if(distanceTravelled >= 50.0f)
{
objectsMoving.Remove(objectsMoving[objectsMoving.Count]);
}
The idea in general is to move the last item object from the list to the start of the list and keep moving the objects all the time but each time the last object in the list is distanceTravelled >= 50.0f move it to the start of the list. Same idea as cyclic if I'm not wrong.
c# unity3d
add a comment |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveObjects : MonoBehaviour
{
public float speed = 3f;
private GameObject objectstoMove;
private List<GameObject> objectsMoving = new List<GameObject>();
private float distanceTravelled = 0;
private Vector3 lastPosition;
// Use this for initialization
public void Init()
{
objectstoMove = GameObject.FindGameObjectsWithTag("Test");
objectsMoving = new List<GameObject>(objectstoMove);
lastPosition = objectstoMove[objectstoMove.Length].transform.position;
}
// Update is called once per frame
void Update()
{
if (objectstoMove != null)
{
float step = speed * Time.deltaTime;
for (int i = 0; i < objectstoMove.Length; i++)
{
if(distanceTravelled >= 50.0f)
{
objectsMoving.Remove(objectsMoving[objectsMoving.Count]);
}
objectsMoving[i].transform.Translate((objectsMoving[i].transform.up + objectsMoving[i].transform.forward) * step);
distanceTravelled += Vector3.Distance(objectsMoving[objectsMoving.Count].transform.position, lastPosition);
lastPosition = objectsMoving[objectsMoving.Count].transform.position;
}
}
}
}
In this part I want to take the last object in the list and move it to the start of the list:
if(distanceTravelled >= 50.0f)
{
objectsMoving.Remove(objectsMoving[objectsMoving.Count]);
}
The idea in general is to move the last item object from the list to the start of the list and keep moving the objects all the time but each time the last object in the list is distanceTravelled >= 50.0f move it to the start of the list. Same idea as cyclic if I'm not wrong.
c# unity3d
1
objectsMoving.Count
is out of range. You need a -1.
– Draco18s
Nov 16 '18 at 21:57
add a comment |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveObjects : MonoBehaviour
{
public float speed = 3f;
private GameObject objectstoMove;
private List<GameObject> objectsMoving = new List<GameObject>();
private float distanceTravelled = 0;
private Vector3 lastPosition;
// Use this for initialization
public void Init()
{
objectstoMove = GameObject.FindGameObjectsWithTag("Test");
objectsMoving = new List<GameObject>(objectstoMove);
lastPosition = objectstoMove[objectstoMove.Length].transform.position;
}
// Update is called once per frame
void Update()
{
if (objectstoMove != null)
{
float step = speed * Time.deltaTime;
for (int i = 0; i < objectstoMove.Length; i++)
{
if(distanceTravelled >= 50.0f)
{
objectsMoving.Remove(objectsMoving[objectsMoving.Count]);
}
objectsMoving[i].transform.Translate((objectsMoving[i].transform.up + objectsMoving[i].transform.forward) * step);
distanceTravelled += Vector3.Distance(objectsMoving[objectsMoving.Count].transform.position, lastPosition);
lastPosition = objectsMoving[objectsMoving.Count].transform.position;
}
}
}
}
In this part I want to take the last object in the list and move it to the start of the list:
if(distanceTravelled >= 50.0f)
{
objectsMoving.Remove(objectsMoving[objectsMoving.Count]);
}
The idea in general is to move the last item object from the list to the start of the list and keep moving the objects all the time but each time the last object in the list is distanceTravelled >= 50.0f move it to the start of the list. Same idea as cyclic if I'm not wrong.
c# unity3d
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveObjects : MonoBehaviour
{
public float speed = 3f;
private GameObject objectstoMove;
private List<GameObject> objectsMoving = new List<GameObject>();
private float distanceTravelled = 0;
private Vector3 lastPosition;
// Use this for initialization
public void Init()
{
objectstoMove = GameObject.FindGameObjectsWithTag("Test");
objectsMoving = new List<GameObject>(objectstoMove);
lastPosition = objectstoMove[objectstoMove.Length].transform.position;
}
// Update is called once per frame
void Update()
{
if (objectstoMove != null)
{
float step = speed * Time.deltaTime;
for (int i = 0; i < objectstoMove.Length; i++)
{
if(distanceTravelled >= 50.0f)
{
objectsMoving.Remove(objectsMoving[objectsMoving.Count]);
}
objectsMoving[i].transform.Translate((objectsMoving[i].transform.up + objectsMoving[i].transform.forward) * step);
distanceTravelled += Vector3.Distance(objectsMoving[objectsMoving.Count].transform.position, lastPosition);
lastPosition = objectsMoving[objectsMoving.Count].transform.position;
}
}
}
}
In this part I want to take the last object in the list and move it to the start of the list:
if(distanceTravelled >= 50.0f)
{
objectsMoving.Remove(objectsMoving[objectsMoving.Count]);
}
The idea in general is to move the last item object from the list to the start of the list and keep moving the objects all the time but each time the last object in the list is distanceTravelled >= 50.0f move it to the start of the list. Same idea as cyclic if I'm not wrong.
c# unity3d
c# unity3d
asked Nov 16 '18 at 20:53
Dubi DuboniDubi Duboni
450110
450110
1
objectsMoving.Count
is out of range. You need a -1.
– Draco18s
Nov 16 '18 at 21:57
add a comment |
1
objectsMoving.Count
is out of range. You need a -1.
– Draco18s
Nov 16 '18 at 21:57
1
1
objectsMoving.Count
is out of range. You need a -1.– Draco18s
Nov 16 '18 at 21:57
objectsMoving.Count
is out of range. You need a -1.– Draco18s
Nov 16 '18 at 21:57
add a comment |
1 Answer
1
active
oldest
votes
Do something like this:
if(distanceTravelled >= 50.0f)
{
var moveToFirst = objectsMoving.Last();
objectsMoving.RemoveAt(objectsMoving.Count-1);
objectsMoving.Insert(0, moveToFirst);
}
1
Why not.Remove(moveToFirst)
? Or alternatively,moveToFirst = objectsMoving[objectsMoving.Count-1]
– Draco18s
Nov 16 '18 at 21:59
@Draco18s.Remove
will remove the first instance of the specified value. So if there's a duplicate, you're not removing the item at the last index. Probably not an issue in this case (though it could be), but something to be aware of.
– Rufus L
Nov 16 '18 at 22:31
1
If you swapped the order of operations, you could do it in one less line:objectsMoving.Insert(0, objectsMoving.Last()); objectsMoving.RemoveAt(objectsMoving.Count-1);
– Rufus L
Nov 16 '18 at 22:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345227%2fhow-can-i-move-item-from-end-of-list-to-the-start%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Do something like this:
if(distanceTravelled >= 50.0f)
{
var moveToFirst = objectsMoving.Last();
objectsMoving.RemoveAt(objectsMoving.Count-1);
objectsMoving.Insert(0, moveToFirst);
}
1
Why not.Remove(moveToFirst)
? Or alternatively,moveToFirst = objectsMoving[objectsMoving.Count-1]
– Draco18s
Nov 16 '18 at 21:59
@Draco18s.Remove
will remove the first instance of the specified value. So if there's a duplicate, you're not removing the item at the last index. Probably not an issue in this case (though it could be), but something to be aware of.
– Rufus L
Nov 16 '18 at 22:31
1
If you swapped the order of operations, you could do it in one less line:objectsMoving.Insert(0, objectsMoving.Last()); objectsMoving.RemoveAt(objectsMoving.Count-1);
– Rufus L
Nov 16 '18 at 22:33
add a comment |
Do something like this:
if(distanceTravelled >= 50.0f)
{
var moveToFirst = objectsMoving.Last();
objectsMoving.RemoveAt(objectsMoving.Count-1);
objectsMoving.Insert(0, moveToFirst);
}
1
Why not.Remove(moveToFirst)
? Or alternatively,moveToFirst = objectsMoving[objectsMoving.Count-1]
– Draco18s
Nov 16 '18 at 21:59
@Draco18s.Remove
will remove the first instance of the specified value. So if there's a duplicate, you're not removing the item at the last index. Probably not an issue in this case (though it could be), but something to be aware of.
– Rufus L
Nov 16 '18 at 22:31
1
If you swapped the order of operations, you could do it in one less line:objectsMoving.Insert(0, objectsMoving.Last()); objectsMoving.RemoveAt(objectsMoving.Count-1);
– Rufus L
Nov 16 '18 at 22:33
add a comment |
Do something like this:
if(distanceTravelled >= 50.0f)
{
var moveToFirst = objectsMoving.Last();
objectsMoving.RemoveAt(objectsMoving.Count-1);
objectsMoving.Insert(0, moveToFirst);
}
Do something like this:
if(distanceTravelled >= 50.0f)
{
var moveToFirst = objectsMoving.Last();
objectsMoving.RemoveAt(objectsMoving.Count-1);
objectsMoving.Insert(0, moveToFirst);
}
answered Nov 16 '18 at 21:01
GiolliaGiollia
794
794
1
Why not.Remove(moveToFirst)
? Or alternatively,moveToFirst = objectsMoving[objectsMoving.Count-1]
– Draco18s
Nov 16 '18 at 21:59
@Draco18s.Remove
will remove the first instance of the specified value. So if there's a duplicate, you're not removing the item at the last index. Probably not an issue in this case (though it could be), but something to be aware of.
– Rufus L
Nov 16 '18 at 22:31
1
If you swapped the order of operations, you could do it in one less line:objectsMoving.Insert(0, objectsMoving.Last()); objectsMoving.RemoveAt(objectsMoving.Count-1);
– Rufus L
Nov 16 '18 at 22:33
add a comment |
1
Why not.Remove(moveToFirst)
? Or alternatively,moveToFirst = objectsMoving[objectsMoving.Count-1]
– Draco18s
Nov 16 '18 at 21:59
@Draco18s.Remove
will remove the first instance of the specified value. So if there's a duplicate, you're not removing the item at the last index. Probably not an issue in this case (though it could be), but something to be aware of.
– Rufus L
Nov 16 '18 at 22:31
1
If you swapped the order of operations, you could do it in one less line:objectsMoving.Insert(0, objectsMoving.Last()); objectsMoving.RemoveAt(objectsMoving.Count-1);
– Rufus L
Nov 16 '18 at 22:33
1
1
Why not
.Remove(moveToFirst)
? Or alternatively, moveToFirst = objectsMoving[objectsMoving.Count-1]
– Draco18s
Nov 16 '18 at 21:59
Why not
.Remove(moveToFirst)
? Or alternatively, moveToFirst = objectsMoving[objectsMoving.Count-1]
– Draco18s
Nov 16 '18 at 21:59
@Draco18s
.Remove
will remove the first instance of the specified value. So if there's a duplicate, you're not removing the item at the last index. Probably not an issue in this case (though it could be), but something to be aware of.– Rufus L
Nov 16 '18 at 22:31
@Draco18s
.Remove
will remove the first instance of the specified value. So if there's a duplicate, you're not removing the item at the last index. Probably not an issue in this case (though it could be), but something to be aware of.– Rufus L
Nov 16 '18 at 22:31
1
1
If you swapped the order of operations, you could do it in one less line:
objectsMoving.Insert(0, objectsMoving.Last()); objectsMoving.RemoveAt(objectsMoving.Count-1);
– Rufus L
Nov 16 '18 at 22:33
If you swapped the order of operations, you could do it in one less line:
objectsMoving.Insert(0, objectsMoving.Last()); objectsMoving.RemoveAt(objectsMoving.Count-1);
– Rufus L
Nov 16 '18 at 22:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345227%2fhow-can-i-move-item-from-end-of-list-to-the-start%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
objectsMoving.Count
is out of range. You need a -1.– Draco18s
Nov 16 '18 at 21:57