- Home /
Scroll Rect Snapping
I followed this tutorial (https://www.youtube.com/watch?v=jWbAaBEQpvE#t=515.060786) and have gone through it several times from start to finish, but cannot get my scroll rect to snap. The scroll rect goes on forever (I have it set set to unrestricted according to the tutorial). What could I be doing wrong?
Here's a video of my issue in action and a look at my hierarchy - https://youtu.be/YGUOsvauQhc
Here's my code as well:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScrollRectSnap : MonoBehaviour {
public RectTransform panel;
public Button [] bttn;
public RectTransform center;
private float[] distance;
private bool dragging = false;
private int bttnDistance;
private int minButtonNum;
void Start(){
int bttnLength = bttn.Length;
distance = new float[bttnLength];
bttnDistance=(int)Mathf.Abs(bttn[1].GetComponent<RectTransform>().anchoredPosition.x - bttn[0].GetComponent<RectTransform>().anchoredPosition.x);
}
void Update(){
for (int i = 0; i < bttn.Length; i++) {
distance [i] = Mathf.Abs (center.transform.position.x - bttn [i].transform.position.x);
}
float minDistance = Mathf.Min (distance);
for (int a = 0; a < bttn.Length; a++) {
if (minDistance == distance [a]) {
minButtonNum = a;
}
}
if (!dragging) {
LerpToBttn (minButtonNum * -bttnDistance);
}
}
void LerpToBttn(int position){
float newX = Mathf.Lerp (panel.anchoredPosition.x, position, Time.deltaTime * 5f);
Vector2 newPosition = new Vector2 (newX, panel.anchoredPosition.y);
panel.anchoredPosition = newPosition;
}
public void StartDrag(){
dragging = true;
}
public void EndDrag(){
dragging = false;
}
}
Your "CenterToCompare" is inside the scroll panel. Thus it will move with the scroll panel and cannot compare the distance.
Put it outside the scroll panel. In your case put it under "Panel" game object...
Well. Just get it out of the ScrollRect. This object is only for checking the distance to each button. It shoud NOT move, because otherwise it will give you wrong distances...
If you are using this specific object for something, then make another one (Empty) and put it as CenterToCompare in the GameController...
Is the scroll rect moving when you drag the buttons in playmode?
I suggest you to watch the part of the tutorial 1, when i create the UI's, to see if everything is correct. You have missed something but i cant tell what.
Or pack your project and send it to me. I will take a look
This is really bizarre! I created a new project and went through the tutorial and was able to get it to work, but when I attempt to implement it into my current project it does not work. What could causing this? Something in my setup?
Well, unfortunately i can't really tell.
It might be wrong referencing of the stuff in the Inspector. Because if you have some errors in the code, you would have seen them..
Check if everything that you reference in the Inspector is the same and if the hierarchy of the objects is similar..
Your answer
![](https://koobas.hobune.stream/wayback/20220612085042im_/https://answers.unity.com/themes/thub/images/avi.jpg)