简体   繁体   中英

Cloning Exact Position of Object

I'm trying to make a BTD game. For my towers, I've put in a Upgrade button on each GameObject and attached it to the GameObject (The tower) itself. Everytime a new tower is selected, the button for the new tower appears and the button for the previously selected tower disappears. However, my problem is that the button's position changes everytime I select a new tower by a slight margin (enough to look out of alignment), even though the button's position states the same X, Y, and Z value. I'm currently using this code to tell the position of the button for the GameTower:

clone.transform.GetChild(9).GetChild(0).localPosition = new Vector3 (-50, 17, 0).

Is there anyway to anchor down the button's position even through cloning (the previous sentence of code isn't working? I'd be happy to expand and share my code if needed.

I don't know why the position would suddenly change. But I think you should look at some of these points.

What could happen:

  1. The parent of the buttons changes it position.
  2. Other code may interfere and change the position without your knowledge.

It's also not correct to use getchild.getchild.localposition. You should try to make a script that knows its child so you can get the child directly from that script.

something like this

Script on parent

public GameObject ButtonsContainer
{
   get
   {
       return buttonsContainer;
   }
}

[SerializeField] private GameObject buttonsContainer;

Script that changes the position on awake

[SerializeField] private Script parentScript;
[SerializeField] private Vector3 buttonPosition;
void Awake()
{
     parentScript.ButtonsContainer.transform.localPosition = buttonPosition;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM