Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by El-Deiablo · Jul 18, 2016 at 12:01 PM · scrollscrollingrectsnapsnapping

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;
 
     }
 }

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image instruct9r · Jul 18, 2016 at 01:23 PM 0
Share

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...

avatar image El-Deiablo instruct9r · Jul 18, 2016 at 06:31 PM 0
Share

This is how I have it setup :(

avatar image instruct9r El-Deiablo · Jul 18, 2016 at 08:52 PM 0
Share

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...

Show more comments
avatar image instruct9r · Jul 19, 2016 at 12:41 PM 0
Share

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

avatar image El-Deiablo instruct9r · Jul 25, 2016 at 12:44 PM 0
Share

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?

avatar image instruct9r El-Deiablo · Jul 25, 2016 at 02:54 PM 0
Share

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..

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

48 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

scrollrect jump to screen 0 Answers

Create Grid and Snap Rigidbodies to Grid 1 Answer

Camera animation issue 0 Answers

An UI element will follow the mouse and snap to a grid, but why is the grid shifted after every game start? 0 Answers

white line when snapping objects 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges