I am trying to update a sprite using playerprefs and the Dropdown UI Element. Upon selecting Day or night time will determine which sprite is rendered upon the scene restarting/starting. However when I select Day or night it doesn't work. It's not changing the sprite upon load. I have the sprites set in the inspector correctly as-well.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class repeatingBackground : MonoBehaviour {
private BoxCollider2D groundCollider;
private float groundHorizontalLength;
private Sprite spriteRenderer;
public Sprite daySprite;
public Sprite nightSprite;
private int bgImg;
void Start () {
bgImg = PlayerPrefs.GetInt ("backgroundImg");
spriteRenderer = GetComponent<Sprite>();
if (bgImg == 1 || bgImg == 0)
spriteRenderer = daySprite;
else if (bgImg == 2)
spriteRenderer = nightSprite;
groundCollider = GetComponent<BoxCollider2D> ();
groundHorizontalLength = groundCollider.size.x;
}
// Update is called once per frame
void Update () {
if (transform.position.x < -groundHorizontalLength) {
RepositionBackground ();
}
}
public void OnValueChanged(Dropdown value)
{
PlayerPrefs.SetInt ("backgroundImg", value.value);
PlayerPrefs.Save ();
}
private void RepositionBackground()
{
Vector2 groundOffset = new Vector2 (groundHorizontalLength * 2f, 0);
transform.position = (Vector2)transform.position + groundOffset;
}
}