I’m creating a queue, and I can’t develop function logic for the previous object to assume the new position released - C#

Asked

Viewed 114 times

2

I’m using unity3D, so my queue consists of Gameobjects, are 7 Gameobjects lined up next to each other and my purpose with this code is that every click of the user - "Simplemove function" - the objects move forward one position, except the last one, this must take the last position.

In Unity it is possible to associate the Gameobjects by the interface, so consider the two full arrays already at the beginning.

I would like help to understand and develop the logic in "Simplemove function" make this drive. Thank you

using UnityEngine;
using System.Collections;

public class StairsController : MonoBehaviour {

    public GameObject[] degrau;
    Vector3[] positionArray = new Vector3[7];

    private int i = 6;
    private int a = 6;

    // Use this for initialization
    void Start () {
        positionArray [i-6] = degrau[a-6].transform.position; 
        Debug.Log (positionArray [i-6]);

        positionArray [i-5] = degrau[a-5].transform.position;
        Debug.Log (positionArray [i-5]);

        positionArray [i-4] = degrau[a-4].transform.position;
        Debug.Log (positionArray [i-4]);

        positionArray [i-3] = degrau[a-3].transform.position;
        Debug.Log (positionArray [i-3]);

        positionArray [i-2] = degrau[a-2].transform.position;
        Debug.Log (positionArray [i-2]);

        positionArray [i-1] = degrau[a-1].transform.position;
        Debug.Log (positionArray [i-1]);

        positionArray [i] = degrau[a].transform.position;
        Debug.Log (positionArray [i]);
    }

    public void SimpleMov (){
        degrau [a].transform.position = positionArray [i - 1];
    }
}

1 answer

1

Great tip: only use array for one-size structures that do not need modification, for variable sizes and other strings use "List" is much easier and you will have great processing savings.

You will need to

using System.Collections.Generic;

To start

List<GameObject> degrau= new List<GameObject>();

To add elements

degrau.Add(**teu degrau aqui**);

To remove the element has several options, but this should be the most suitable.

degrau.RemoveAt(int index)

and

degrau.Clear()

to clean up

More information about the list https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110). aspx

I don’t have a list-specific video, but I use it in this video here https://www.youtube.com/watch?v=5U3d9iwHm2I

  • How can I move objects between the positions of this list?

  • You said you have elements in sequence, 1,2,3,4 You want them to move forward and the latter hold the last position. 2,3,4. Just remove the first one. If you want to swap objects, there are several functions in the link I sent at the end, various types of Sort. And if you want you can do a function as it is here: http://stackoverflow.com/questions/2094239/swap-two-items-in-listt

  • If you want the FIRST to take the last position, as in an escalator you can try to use Queue (first in, first out). https://msdn.microsoft.com/en-us/library/7977ey2c(v=vs.100). aspx

  • In this case, if you want to keep the size fixed, you can use array, then just put an index to tell where the "start of the queue".

  • Yeah, I talked to a guy at Stack Overflow in English to discuss this problem and it seemed a lot more complicated than initially, we solved it using two of the guys who went through the list and always started from a forward position, but also zeroed out when they found the last element.

  • Strange, it seems to me very simple the solution. Is there any information that you passed on to him and did not pass it on to us? Or did you pass it on differently?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.