I am trying to make a player list but I can’t seem to be able to make the new player button to take the correct position for some reason.
here is the code:
public void ConfirmPlayer(GameObject readyPlayerBox)
{
Vector3 scale = new Vector3(1f, 1f, 1f);
Vector3 newBoxPos = new Vector3(readyPlayerBox.transform.position.x + Players.Count * 120, 0, 0);
GameObject playerBox = new GameObject();
playerBox = GameObject.Instantiate(readyPlayerBox);
playerBox.transform.name = Players.Last().Name + " player button";
playerBox.transform.SetParent(readyPlayerBox.transform.parent);
playerBox.transform.position = readyPlayerBox.transform.position;
playerBox.transform.position = newBoxPos;
playerBox.transform.localScale = scale;
}
help would be much appreciated if you need some more info about it feel free to ask
What is the “correct position”?
What is currently happening?
Does Players.Count ever increment?
By the way, there are some “Code:” buttons in the post editor that you can use to paste pre-formated code text.
Hey thank you for replying
the correct pos is the old box location + 120
this is what currently happening
it is going to a much lower y and a different x than what I intended it to be
this is the pos of the original button and to the right of the pic above is the pos of the new one
Players.count increases every time a new player is made, Players is a list of players, every time a new player is made it is added to that list
If this is an object on a canvas, then you need to modify the RectTransform, not the Transform. As a first step, try modifying the anchoredPosition of the RectTransform.
Maybe something like this will work:
public void ConfirmPlayer(GameObject readyPlayerBox)
{
Vector3 scale = new Vector3(1f, 1f, 1f);
RectTransform originalRectTransform = readyPlayerBox.GetComponent<RectTransform>();
//You were also setting the Y position to 0 in your original code, rather than taking it from readyPlayerBox
Vector2 newBoxPos = new Vector2(originalRectTransform.anchoredPosition.x + Players.Count * 120, originalRectTransform.anchoredPosition.y);
GameObject playerBox = Instantiate(readyPlayerBox);
RectTransform newRectTransform = playerBox.GetComponent<RectTransform>();
playerBox.transform.name = Players.Last().Name + " player button";
playerBox.transform.SetParent(readyPlayerBox.transform.parent);
newRectTransform.localScale = scale;
newRectTransform.anchoredPosition = newBoxPos;
}
It worked ty so much, I can’t believe I didn’t figure that out on my own lol I have been searching for like an hour, and its pretty basic