Author Topic: some problems with UI  (Read 1484 times)

krvc

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 16
    • View Profile
some problems with UI
« on: October 18, 2016, 12:16:21 PM »
Hi. I try create HP bar to unit, but again and again something wrong.

2 scripts on Player
create hp bar

   public static GameObject playerHPpanel;

   static UIHealthInfo _UIHealthInfo;


    // Use this for initialization
   void Start()
    {
       
        if (tno.isMine)
        {
            // not network sample
            //     GameObject slider = Instantiate<GameObject>(Resources.Load<GameObject>("HPpanelUI"));
            //     slider.transform.SetParent(GameObject.Find("MainCanvas").transform);
            //    _UIHealthInfo = slider.GetComponent<UIHealthInfo>();
            //     _UIHealthInfo.Target = this;
         
            // network sample
           Vector3 pos = new Vector3(37.0f, 4, 38);
           Quaternion rot = Quaternion.Euler(0, 45, 0);
           TNManager.Instantiate(2, "MenuCreate", "HPpanelUI", false, pos, rot);
           _UIHealthInfo.Target = this;

         }

    }

    [RCC]
    static GameObject MenuCreate(GameObject prefab, Vector3 pos, Quaternion rot)
    {

        // Instantiate the prefab
        playerHPpanel = prefab.Instantiate();
        // Set the position and rotation based on the passed values
        Transform t = playerHPpanel.transform;
        t.position = pos;
        t.rotation = rot;
       
         playerHPpanel.transform.SetParent(GameObject.Find("MainCanvas").transform);
        _UIHealthInfo = playerHPpanel.GetComponent<UIHealthInfo>();
       

        return playerHPpanel;
    }

Next script change position.
public class UIHealthInfo : MonoBehaviour {

   
    public HealthInfo Target { get; internal set; }
   
    // Use this for initialization
    UIHealthInfo _UIHealthInfo;

    void Start () {
   
   }

    void Update()

    {


        if (Target == null)
            return;
        Vector3 oldPos = new Vector3(Target.transform.position.x, Target.transform.position.y + Target.GetComponent<Collider>().bounds.size.y*3, Target.transform.position.z);
        Vector3 newPos = Camera.main.WorldToScreenPoint(oldPos);
        GetComponent<RectTransform>().position = newPos;


    }

}

its work in single, but in multiplayer something wrong, maybe problem with Target.

I try to go on easy way also, i off it all, and create Canvas into player unit. Its have problem too.
 Function DestroySelf - work and destroy unit, but after added Canvas with hp panel, DestroySelf 100% crush Unity or game.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: some problems with UI
« Reply #1 on: October 19, 2016, 06:36:58 PM »
You're setting a static value _UIHealthInfo every time your RCC gets called. Unless you only ever have 1 unit on screen, this seems wrong to me.

UI needs to be local. You shouldn't be instantiating it like that. Create your game unit using TNManager.Instantiate -- and a script on the game unit should be the one that creates a UI element for it locally. Why are you trying to network it?