Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

GUI position above selected Npc

Discussion in 'Scripting' started by TheSenpaiCode, May 17, 2018.

  1. TheSenpaiCode

    TheSenpaiCode

    Joined:
    Jan 21, 2016
    Posts:
    50
    Hello,
    I'm trying to drag and drop a NPC into the script and have the GUI target that NPC so the dialogue appears above his head. If I have one NPC this works but as soon as I attach the script to another NPC the dialogue for both NPC appear above the second NPC's head. I know this should be simple and have tried a few ways that should fix this issue.

    Code (CSharp):
    1.    public Transform GUIBackGround;
    2.     public Transform NPCCharacter;
    3.  
    4.     void Start () {
    5.  
    6.     }
    7.  
    8.     void Update () {
    9.         Vector3 Pos = Camera.main.WorldToScreenPoint(NPCCharacter.position);
    10.         Pos.y += 175;
    11.         GUIBackGround.position = Pos;
    12. }
    13.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Is GUIBackGround referencing the same transform on both NPC's?
     
  3. TheSenpaiCode

    TheSenpaiCode

    Joined:
    Jan 21, 2016
    Posts:
    50
    Yeah GUIBackGround is referencing the GUI canvas for the dialogue system in the Heirarchy that both NPC's are using for the Dialogue.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So the way the script is written, whichever runs update last will set GUIBackGround.position to that character's position. You're going to need a way to have your UI object handle multiple positions at the same time. The way I do it is have a UI window for each NPC instead of a shared one, but there is certainly ways to use a shared UI object instead (but not this way).
     
  5. TheSenpaiCode

    TheSenpaiCode

    Joined:
    Jan 21, 2016
    Posts:
    50
    I was thinking of having the UI switch to the NPC that is currently active. Well is what I'm trying to go for. JAny idea how I would go about coding that in?