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. Dismiss Notice

C# display message in GUI on trigger enter.

Discussion in 'Scripting' started by Christo_Katkos, Oct 18, 2014.

  1. Christo_Katkos

    Christo_Katkos

    Joined:
    Jan 7, 2014
    Posts:
    20
    I have this script that debug.logs a message when I enter the trigger, there are 3 different messages that can display, depending on the trigger I collide with.

    Code (CSharp):
    1. public class WeaponStoreRack : MonoBehaviour
    2. {
    3.     //Creates a Enum and dropdown to select weapon
    4.     public enum Weapons {bow=0, sword=1, staff=3};
    5.     public Weapons selectedWeapon;
    6.    
    7.     //when player triggers Rack, get the selected weapon and display msg
    8.     void OnTriggerEnter(Collider other)
    9.     {
    10.         if (other.gameObject.tag == "Player" && selectedWeapon == Weapons.bow)
    11.         {
    12.             Debug.Log("Ah! The bow! a Ranged weapon to get your foes before they get you! There once was a Archer that could shoot 3 arrows at once! That will be $" + bowPrice);
    13.         }
    14.        
    15.         if (other.gameObject.tag == "Player" && selectedWeapon == Weapons.sword)
    16.         {
    17.             Debug.Log("Ah! The sword! a Sharp blade to stab your foes with! There once was a Soldier that could slash an entire group of foes at once! That will be $" + swordPrice);
    18.         }
    19.        
    20.         if (other.gameObject.tag == "Player" && selectedWeapon == Weapons.staff)
    21.         {
    22.             Debug.Log("Ah! The staff! a Light weapon with great reach! There once was a man that could deflect projectiles with a staff! That will be $" + staffPrice);
    23.         }
    24.        
    25.     }
    26. }
    How do I get the Debug.log msg from this script into the script that goes onto my GUI, so it will display when the trigger conditions are met?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Do you have a script for displaying text?

    Does the script for displaying text have a method on it to call and tell it a message to display?

    If so, get a reference to that script, and OnTriggerEnter call that method.
     
  3. Christo_Katkos

    Christo_Katkos

    Joined:
    Jan 7, 2014
    Posts:
    20
    Here is what I have:

    this goes onto the WeaponRack, which is basically the shop object

    Code (CSharp):
    1. public class WeaponStoreRack : MonoBehaviour
    2. {
    3.     //Creates a Enum and dropdown to select weapon
    4.     public enum Weapons {bow=0, sword=1, staff=3};
    5.     public Weapons selectedWeapon;
    6.    
    7.     //Weapon Prices
    8.     public int bowPrice = 5;
    9.     public int swordPrice = 10;
    10.     public int staffPrice = 15;
    11.    
    12.     //Popup Info message to display
    13.     public string bowDisplayMessage = "Ah! The bow! a Ranged weapon to get your foes before they get you! There once was a Archer that could shoot 3 arrows at once! That will be $";
    14.     public string swordDisplayMessage;
    15.     public string staffDisplayMessage;
    the this goes onto the GUI:

    Code (CSharp):
    1. public class PopupInfo : MonoBehaviour
    2. {
    3.     public WeaponStoreRack weaponStoreRack;
    4.     public string bowMessage;
    5.  
    6.     void Awake ()
    7.     {
    8.         weaponStoreRack = GameObject.Find("WeaponRack").GetComponent<WeaponStoreRack>();
    9.         weaponStoreRack.bowDisplayMessage = bowMessage;
    10.     }
    11.    
    12.     void Update ()
    13.     {
    14.         weaponStoreRack.bowDisplayMessage = bowMessage;
    15.        
    16.         guiText.text = "test " + bowMessage;
    17.     }
    when I run the game all that shows in the GUI is test and the + bowMessage is nowhere to be seen?
     
  4. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    I think you wanted to do
    Code (csharp):
    1. bowMessage = weaponStoreRack.bowDisplayMessage;
    instead of the other way around. Currently you are using your empty bowMessage of PopupInfo and are displaying that empty message in your guiText.

    Then instead /additionally of a Debug.Log you can input your trigger string into one string variable(currentDisplayMessage) of weaponStoreRack which you can display in you guiText of PopupInfo