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:
error CS1503: Argument 1: cannot convert from 'System.Collections.Generic.Queue<UnityEngine.GameObject>' to 'UnityEngine.Object'
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?)
using UnityEngine;
using System.Collections.Generic;
public class BackgroundLoop : MonoBehaviour
{
public Queue<GameObject> backgrounds;
// Start is called before the first frame update
void Start()
{
SpawnBackground();
}
// Update is called once per frame
void Update()
{
//SpawnBackground();
}
void SpawnBackground()
{
for (int i = 0; i < backgrounds.Count; i++) {
float spacing = 10;
Vector2 pos = new Vector3(0, spacing * i);
Instantiate (backgrounds, pos, Quaternion.identity);
if (backgrounds.transform.position.y > -10f)
{
// Shift backgrounds[0] to the back in array
backgrounds.Dequeue();
}
}
}
void ShiftObject()
{
}
}
how do i make a thread myself?
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.
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.
using UnityEngine;
using System.Collections.Generic;
public class BackgroundLoop : MonoBehaviour
{
public List<GameObject> background;
// Start is called before the first frame update
void Start()
{
SpawnBackground();
}
// Update is called once per frame
void Update()
{
Move(background, 0, -1);
}
// Spawn the backgrounds
void SpawnBackground()
{
for (int i = 0; i < background.Count; i++) {
float spacing = 10;
Vector2 pos = new Vector2(0, spacing * i);
Instantiate (background[i], pos, Quaternion.identity);
}
}
// Shift the background back at the end in the list
public List<GameObject> Move (List<GameObject> list, int oldIndex, int newIndex)
{
if (background[0].transform.position.y <= -10f)
{
var old = list[oldIndex];
list.RemoveAt(oldIndex);
list.Insert(newIndex, old);
[B] // Also shift objects in scene to new position [/B]
Debug.Log("This is the list -> " + Move(background, 0, -1));
Debug.Log("This is the position of the background -> " + background[0].transform.position.y);
print("Random check");
}
return list;
}
}
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.
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.
background[0].transform.position = new Vector2(0, 5 * background.Count);
Why my backgrounds not always shifting behind each other.
using UnityEngine;
using System.Collections.Generic;
public class BackgroundLoop : MonoBehaviour
{
public float spacing = 10;
public float movSpeed;
public GameObject backgroundPrefab;
public int backgroundCount;
private List<GameObject> backgrounds = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
SpawnBackground();
}
// Update is called once per frame
void Update()
{
Move();
}
// Spawn the backgrounds
void SpawnBackground()
{
for (int i = 0; i < backgroundCount; i++) {
Vector2 pos = new Vector2(0, spacing * i);
GameObject newBackground = Instantiate (backgroundPrefab, pos, Quaternion.identity);
backgrounds.Add(newBackground);
}
}
void Move()
{
// Foreach item in backgrounds apply Vector2
foreach (var item in backgrounds)
{
item.transform.Translate(new Vector2(0, -5) * movSpeed * Time.deltaTime);
}
// If position is bigger than...
if (backgrounds[0].transform.position.y <= -10f)
{
// Shift the backgrounds
GameObject closestBg = backgrounds[0];
closestBg.transform.position = new Vector2(0, 10 * backgrounds.Count);
backgrounds.Remove(closestBg);
backgrounds.Add(closestBg);
}
}
}