• 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 justadeveloper · Jan 09, 2016 at 10:45 PM · uipositionrandomrecttransformanchor

this code doesnt work when the buttons anchor are not same(custom anchor)

this code works only if the buttons have same anchor presets but it doesnt work when buttons have different anchor presets(custom) how to make this work?

 using UnityEngine;
 using System.Collections;
 
 
 public class ShuffleChoice : MonoBehaviour 
 {
     public GameObject option1;
     public GameObject option2;
     public GameObject option3;
     public GameObject option4;
     
     bool stat2=false;
     bool stat3=false;
     bool stat4=false;
 
     public float x;
     public float y;
     public int mover;
     public int before=1;
     public int movequestion=0;
 
     public int count=1;
 
     // Use this for initialization
     void Start () 
     {
         RectTransform choice1 = option1.GetComponent<RectTransform> ();
         RectTransform choice2 = option2.GetComponent<RectTransform> ();
         RectTransform choice3 = option3.GetComponent<RectTransform> ();
         RectTransform choice4 = option4.GetComponent<RectTransform> ();
 
         x = choice1.anchoredPosition.x;
         y = choice1.anchoredPosition.y;
 
 
         do 
         {
             randomcount();
             if (randomcount () == 2 && stat2 == false)
             {
                 if(count==1)
                 {
                     choice1.anchoredPosition = new Vector2 (choice2.anchoredPosition.x,choice2.anchoredPosition.y);
                     choice2.anchoredPosition = new Vector2 (x, y);
                 }
 
                 else if(before==3)
                 {
                     choice3.anchoredPosition = new Vector2 (choice2.anchoredPosition.x, choice2.anchoredPosition.y);
                     choice2.anchoredPosition = new Vector2 (x, y);
                 }
 
                 else if(before==4)
                 {
                     choice4.anchoredPosition = new Vector2 (choice2.anchoredPosition.x, choice2.anchoredPosition.y);
                     choice2.anchoredPosition = new Vector2 (x, y);
                 }
 
                 x = choice2.anchoredPosition.x;
                 y = choice2.anchoredPosition.y;
 
                 count++;
                 stat2 = true;
                 before=2;
                 //Debug.Log ("count=" + count);
             } 
             else if (randomcount () == 3 && stat3 == false)
             {
                 if(count==1)
                 {
                     choice1.anchoredPosition = new Vector2 (choice3.anchoredPosition.x, choice3.anchoredPosition.y);
                     choice3.anchoredPosition = new Vector2 (x, y);
                 }
 
                 else if(before==2)
                 {
                     choice2.anchoredPosition = new Vector2 (choice3.anchoredPosition.x, choice3.anchoredPosition.y);
                     choice3.anchoredPosition = new Vector2 (x, y);
 
                 }
             
                 else if(before==4)
                 {
                     choice4.anchoredPosition = new Vector2 (choice3.anchoredPosition.x, choice3.anchoredPosition.y);
                     choice3.anchoredPosition = new Vector2 (x, y);
                 }
                 
                 x = choice3.anchoredPosition.x;
                 y = choice3.anchoredPosition.y;
 
                 count++;
                 stat3 = true;
                 before=3;
         }
         
         else if (randomcount () == 4 && stat4 == false)
             {
                 if(count==1)
                 {
                     choice1.anchoredPosition = new Vector2 (choice4.anchoredPosition.x, choice4.anchoredPosition.y);
                     choice4.anchoredPosition = new Vector2 (x, y);
                 }
 
             else if(before==2)
             {
                 choice2.anchoredPosition = new Vector2 (choice4.anchoredPosition.x, choice4.anchoredPosition.y);
                 choice2.anchoredPosition = new Vector2 (x, y);
             }
             
             else if(before==3)
             {
                 choice3.anchoredPosition = new Vector2 (choice4.anchoredPosition.x, choice4.anchoredPosition.y);
                 choice4.anchoredPosition = new Vector2 (x, y);
 
             }
 
                 x = choice4.anchoredPosition.x;
                 y = choice4.anchoredPosition.y;
                 
                 count++;
                 stat4 = true;
                 before=4;
                 //Debug.Log ("count=" + count);
         }
         } while(count<=3);
     }
 
     void update()
     {
         //randomcount ();
     }
     
     int randomcount()
     {
         mover = Random.Range (1,5);
         return mover;
     }
 
 }
 
Comment
Add comment
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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[SOLUTION] set UI RectTransform Anchor Presets from code c# 1 Answer

RectTransform.position of Panel always 0 in FullHD 0 Answers

Anchor doesn't affect objects inside of another object 0 Answers

Convert RectTransform anchored position to other position with other anchors 0 Answers

Strange UI scrollbar handle recttransform behaviour,Cant resize clickable area on UI handle 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges