I am trying to use a pool of 3 background sprites, and have them repeat and move back to the top of the stack of objects as they move off camera.
here is what I have so far.
using UnityEngine;
using System.Collections;
public class BackgroundScroller : MonoBehaviour {
public GameObject background;
public GameObject[] bgPool = new GameObject[4];
private float startX = 0;
private float startY = 5;
private float startZ = 2;
// Use this for initialization
void Awake ()
{
for(int i = 0; i < bgPool.Length; i++)
{
bgPool *= (GameObject)Instantiate(background);*
_ bgPool*.SetActive(false);_
_ } _
_ }*_
* void Start()*
* {*
* for(int i = 0; i< bgPool.Length; i++)*
* {*
* if(i == 0)*
* {*
_ bgPool*.SetActive(true);
bgPool.transform.position = new Vector3(startX, startY, startZ);
}
else*
* {
bgPool.SetActive(true);
bgPool.transform.position.x = startX;
bgPool.transform.position.z = startZ;*_
* }*
* }*
* }*
* // Update is called once per frame*
* void Update () {*
* if(Input.GetKeyDown(KeyCode.T))*
* {*
* bgPool[0].SetActive(true);*
* bgPool[0].transform.position = new Vector3(0.0f,0.0f,2.0f);*
* Debug.Log(bgPool[1].renderer.bounds);*
* }*
* }*
}
I planned on it starting at Vector3(0,5,2) so that it starts centered, it starts using the bottom of the sprite and not the center of the image, and the Z is set to 2 so that I can add in parallaxing once I get this one to scroll.
right now it creates the pool of objects, which I am using the awake function to do right now, then I was going to use the start function to place the initial objects on the screen, then i would use the update function to check if they were off camera, and if they were it would move them back to the top.
however right now it returns errors and wont place any on screen. I am able to get it to place one object in the right position, but i cant get it to add another one above that one.
the update function was just set because i was seeing what renderer.bounds would return.
and the start function was where I left off when i finally got fed up and came to ask for help
I've found a minor bug on the second script. Change the last of
– DunkelheitfirstBG.Translate();to: firstBG.transform.position = new Vector3(position, firstBG.transform.position.y, firstBG.transform.position.z);