Question for ambitious people: How can I create multiple background?

Hi

I’m noob in this place, hope I don’t make a mistake posting.

First of all apologise my English writing.
:slight_smile:

I can’t find anything about it, or at least not as accurate .

How can I manage multiple backgrounds?

I’m trying to make a running game. In that game i want to change the background after every 120 seconds or 30 points, like in game like “Jetpack joyride” or “Flappy bird”(day and night) .

How can i do that? Any ideas…??

Generally, I have 6 backgrounds images.

You can work your way through the Unity Learn Site, especially this tutorial about making an infinite runner. There are a lot of related tutorials like making a platformer game. If you go through those, you will very fast come up with an idea how to make the background change.

Generally, it will involve learning to script, Maybe placing colliders (as triggers) and turning Sprite images on and off. But those tutorials cover everything you will need. :wink:

To get you started alongside the tutorials, here is a script that will change a sprite on a SpriteRenderer:

using UnityEngine;

public class ChangeBackground : MonoBehaviour
{
	public SpriteRenderer spriteRenderer;
	public Sprite[] backgrounds;
	public float timerInterval = 30f;

	private float timer;
	private int currentSpriteIndex;

	private void Start()
	{
		SetDefaultBackground();
		ResetTimer();
	}

	private void SetDefaultBackground()
	{
		// Error checking
		if (spriteRenderer == null)
		{
			Debug.LogError("No SpriteRenderer set on ChangeBackground script.");
			return;
		}

		if (backgrounds.Length == 0)
		{
			Debug.LogError("No backgrounds set on ChangeBackground script.");
			return;
		}

		currentSpriteIndex = 0;
		spriteRenderer.sprite = backgrounds[currentSpriteIndex];
	}

	private void ResetTimer()
	{
		timer = timerInterval;
	}

	private void Update()
	{
		if (timer <= 0f)
		{
			ResetTimer();
			OnTimerFinished();
		}
		timer -= Time.deltaTime;
	}

	private void OnTimerFinished()
	{
		// Increment the index by one, but if we go beyond the number of items
		// in the backgrounds array, wrap around and start counting from zero again.
		currentSpriteIndex = (currentSpriteIndex + 1) % backgrounds.Length;
		spriteRenderer.sprite = backgrounds[currentSpriteIndex];
	}

	private void OnValidate()
	{
		// Ensure timer interval stays positive
		// when set through the inspector.
		if (timerInterval <= 0f)
			timerInterval = 0f;
	}
}
  1. Create a new 2D Sprite object in the scene and attach the ChangeBackground component.
  2. Set the SpriteRenderer reference in the inspector and fill the array with background sprite images (make sure they are imported and marked as ‘Sprite’).
  3. See the script cycling through the array, changing the background every x seconds. :slight_smile:

Hey i’m looking for the same thing but with paralax effect on each of my background any idea of what is the best way?,Hi anyway for use multiple paralax background with the same idea?