- Home /
Scrolling one UI image causes every other UI image to start scrolling
I created a background in my game as a UI image. I used this script to create an "infinite" scrolling effect so that it looks like the image is always scrolling upwards:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BackgroundScroll : MonoBehaviour {
public float scrollSpeed;
public Image img;
void Update ()
{
img.material.mainTextureOffset = img.material.mainTextureOffset + new Vector2(0f, Time.deltaTime * (-scrollSpeed / 10));
}
}
First, here's the good part: It works. The background image continuously scrolls upwards.
Now the bad part: Every other UI image in my scene also starts scrolling. I didn't even attach the script to those images. But for some reason, they're scrolling as well.
It's like the line Public Image img caused the script to hold a reference to every UI image in my scene. Why is this happening?
Answer by TheSaviour · Aug 08, 2019 at 06:19 PM
I figured out what's causing the problem. Just like the UI image that I was using for the background, all the other UI images weren't using a material. So Unity treats all of these images as 1 material. So when I change the offset of the background image's material, the script ends up affecting every other object that is using the same material. All I had to do was assign a material to the background UI image. I used the Unlit/Texture shader, since all the other shaders cause the image to turn black.
Your answer
Follow this Question
Related Questions
How do i make my ui elements stay the same size and position on all andorid devices? 1 Answer
UI Canvas Image parented to main camera only moves in x and z 0 Answers
Importing Stripes from Photoshop to Unity 0 Answers
Get mouse click world coordinates "through" UI Raw Image with Render Texture from second camera 2 Answers
Picture loaded from resource corrupted. 0 Answers