Author Topic: Adding an Anchor to a new GameObject.  (Read 2811 times)

FuzzySmurf

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Adding an Anchor to a new GameObject.
« on: December 20, 2013, 06:33:27 PM »
Im trying to add new progressbars to each new instantiated gameObject (soldiers).

These soldiers have a vitality script, and I have this bit into the vitality script.

  1. public GameObject pbar; //my instantiated health bar.
  2. public void Awake()
  3. {
  4.                 pbar = NGUITools.AddChild(GameObject.FindGameObjectWithTag("Panel"), pbar);
  5.  
  6.                 pbar.AddComponent<UIAnchor>();
  7.                 pbar.GetComponent<UIAnchor>().container = this.gameObject;
  8. }

However, it instantiates it the bars at 0,0,0.

Before, I only had addchild, and "pbar.transform.position = this.transform.position;" in Update Script, but I figured their was a better way to do it with Anchors.

any ideas on how to anchor the instantiated GameObject properly?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Adding an Anchor to a new GameObject.
« Reply #1 on: December 21, 2013, 02:52:42 AM »
UIAnchor was deprecated in 3.0.7.

To anchor a widget to a game object, simply set it as the widget's target for left, right, top, and bottom anchor points. I made several videos explaining the new layout system and how it works:

http://www.tasharen.com/forum/index.php?topic=6754.0