Setting localposition to a vector 3 sets everything but the x
I have code that I use to attach a little cursor icon to a UI Button that is part of a menu. When the button is selected, the script attaches the cursor to the object and sets the rect transform of the cursor so it appears just to the left of the menu button.
Here's the code: (the width of the cursor is about 25)
public void OnSelect (BaseEventData eventData)
{
GameObject cursor = MC.CursorInstance();
RectTransform thisRect = cursor.transform as RectTransform;
cursor.transform.SetParent(this.transform);
thisRect.anchorMin = new Vector2(0, 0.5f);
thisRect.anchorMax = new Vector2(0, 0.5f);
thisRect.pivot = new Vector2(0f, 0.5f);
thisRect.localScale = new Vector3(1, 1, 1);
thisRect.localPosition = new Vector3(-thisRect.sizeDelta.x, 0, 0);
}
Not sure why, when selecting the button, the cursor position is set to 13 instead of -25 like I was expecting. Any reason why this might be happening?
Answer by jaredmo · Feb 13, 2016 at 03:43 PM
Since I wanted this position to be relative to the anchor that I set, I needed to set thisRect.anchoredPosition instead of localPosition.
Your answer
Follow this Question
Related Questions
unclickable button when set as child 1 Answer
What is a good way to create dynamic dropdown buttons? 0 Answers
Is it possible to get the screen rect for a button regardless of its parent? 1 Answer
Make a chioce with Selectables and button UI objects 0 Answers
on and off music button in unity 5 0 Answers