Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using Queue for looping background

Discussion in 'Scripting' started by CodeWurm, Jan 10, 2021.

  1. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    I want to use Queue to create an looping background, when first element extends specific range remove at position 0 and at to the end of the queue, I spawn totally around 3 objects only.

    I encounter two errors around Instantiate and transform.position:
    Code (CSharp):
    1.  error CS1503: Argument 1: cannot convert from 'System.Collections.Generic.Queue<UnityEngine.GameObject>' to 'UnityEngine.Object'
    2.  
    3. error CS1061: 'Queue<GameObject>' does not contain a definition for 'transform' and no accessible extension method 'transform' accepting a first argument of type 'Queue<GameObject>' could be found (are you missing a using directive or an assembly reference?)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class BackgroundLoop : MonoBehaviour
    5. {
    6.     public Queue<GameObject> backgrounds;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         SpawnBackground();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         //SpawnBackground();
    18.     }
    19.  
    20.     void SpawnBackground()
    21.     {
    22.          for (int i = 0; i < backgrounds.Count; i++) {
    23.             float spacing = 10;
    24.             Vector2 pos = new Vector3(0, spacing * i);
    25.             Instantiate (backgrounds, pos, Quaternion.identity);
    26.  
    27.             if (backgrounds.transform.position.y > -10f)
    28.             {
    29.                 // Shift backgrounds[0] to the back in array
    30.                 backgrounds.Dequeue();
    31.             }
    32.         }
    33.     }
    34.  
    35.     void ShiftObject()
    36.     {
    37.        
    38.     }
    39. }
    40.  
     
  2. ShadowFaderr

    ShadowFaderr

    Joined:
    Jan 6, 2021
    Posts:
    3
    how do i make a thread myself?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Line 25 you are asking Unity to Instantiate a Queue. That's not possible.

    Line 27 you are asking for the Transform Component of the entire Queue, which make no sense. GameObjects have transforms, not Queues.

    In both cases, dereference or extract the GameObject you want from the queue and then do your operation on that one GameObject.
     
  4. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    In my list, the index changes and is given correctly to the object, but in the scene, the object doesn't move behind the current objects in the list, how can I achieve that.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. public class BackgroundLoop : MonoBehaviour
    6. {
    7.     public List<GameObject> background;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         SpawnBackground();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         Move(background, 0, -1);
    19.     }
    20.  
    21.     // Spawn the backgrounds
    22.     void SpawnBackground()
    23.     {
    24.          for (int i = 0; i < background.Count; i++) {
    25.             float spacing = 10;
    26.             Vector2 pos = new Vector2(0, spacing * i);
    27.             Instantiate (background[i], pos, Quaternion.identity);
    28.         }
    29.     }
    30.  
    31.     // Shift the background back at the end in the list
    32.     public List<GameObject> Move (List<GameObject> list, int oldIndex, int newIndex)
    33.     {
    34.         if (background[0].transform.position.y <= -10f)
    35.         {
    36.             var old = list[oldIndex];
    37.             list.RemoveAt(oldIndex);
    38.             list.Insert(newIndex, old);
    39.          
    40.            [B] // Also shift objects in scene to new position [/B]
    41.  
    42.             Debug.Log("This is the list -> " + Move(background, 0, -1));
    43.             Debug.Log("This is the position of the background -> " + background[0].transform.position.y);
    44.             print("Random check");
    45.         }
    46.         return list;
    47.     }
    48. }
    49.  
     
    Last edited: Jan 11, 2021
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Understand that layering is done in Unity in three ways: UI ordering (order in hierarchy), Sprites (sorting order and sorting layer), and 3D (Z buffering). Shaders can complicate ALL of these ways.
     
  6. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    I fixed most of the stuff already, only moving my background like I do in my list. I got no errors but not each background is moving correctly.
    Code (CSharp):
    1.             background[0].transform.position = new Vector2(0, 5 * background.Count);
    2.  
     
  7. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Why my backgrounds not always shifting behind each other. img2.PNG
     
  8. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class BackgroundLoop : MonoBehaviour
    5. {
    6.     public float spacing = 10;
    7.     public float movSpeed;
    8.     public GameObject backgroundPrefab;
    9.     public int backgroundCount;
    10.     private List<GameObject> backgrounds = new List<GameObject>();
    11.    
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         SpawnBackground();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.        Move();
    22.     }
    23.  
    24.     // Spawn the backgrounds
    25.     void SpawnBackground()
    26.     {
    27.         for (int i = 0; i < backgroundCount; i++) {
    28.             Vector2 pos = new Vector2(0, spacing * i);
    29.             GameObject newBackground = Instantiate (backgroundPrefab, pos, Quaternion.identity);
    30.             backgrounds.Add(newBackground);
    31.         }
    32.     }
    33.  
    34.     void Move()
    35.     {
    36.         // Foreach item in backgrounds apply Vector2
    37.         foreach (var item in backgrounds)
    38.         {
    39.             item.transform.Translate(new Vector2(0, -5) * movSpeed * Time.deltaTime);
    40.         }
    41.  
    42.         // If position is bigger than...
    43.         if (backgrounds[0].transform.position.y <= -10f)
    44.         {
    45.             // Shift the backgrounds
    46.             GameObject closestBg = backgrounds[0];
    47.             closestBg.transform.position = new Vector2(0, 10 * backgrounds.Count);
    48.             backgrounds.Remove(closestBg);
    49.             backgrounds.Add(closestBg);
    50.         }
    51.     }
    52. }