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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing Canvas Text When Player Enters Room

Discussion in 'Scripting' started by TKIZZLE, Dec 2, 2017.

  1. TKIZZLE

    TKIZZLE

    Joined:
    Dec 2, 2017
    Posts:
    6
    I'm totally fresh to Unity and C#, and slightly knowledgeable with JS, so bear with me please.
    I start the player off with a canvas and that is supposed to stay with them through the game, so that they know what to do. I'm trying to make it so that when the player walks through the doorway, there's an invisible collider that changes the text in the canvas to a new message. What am I missing here? If there's anything else you need to help you help me please let me know.

    Code (JavaScript):
    1. import UnityEngine.UI;
    2.  
    3. var TextBoxOnCheck : int = 0;
    4. var MessageBox : GameObject;
    5. var TextBox : GameObject;
    6. var TextMessage : String;
    7.  
    8. function OnTriggerEnter (other : Collider) {
    9.     if(other.gameObject.tag == "Player"){
    10.         TextBoxOnCheck = 1;
    11.         MessageBox.SetActive(true);
    12.         TextBox.GetComponent.<Text>().text = TextMessage;
    13.         TextMessage = "Let's pick our favorite seat at the bar. (Click a stool)";
    14.     }
    15. }
    16.  
    Capture2.JPG Capture.JPG
     
  2. dpgdemos

    dpgdemos

    Joined:
    Apr 28, 2014
    Posts:
    24
  3. TKIZZLE

    TKIZZLE

    Joined:
    Dec 2, 2017
    Posts:
    6
    I'm not sure that's what I'm looking for. I tried implementing your suggestion, but it did not change the text in the canvas. It seems OnControllerColliderHit that it functions more for pushing objects around when walking into them? Or at least that's my understanding. This is what I made (side note I was making this with JS so I tried to change it accordingly.):

    Code (JavaScript):
    1. function OnControllerColliderHit (hit : ControllerColliderHit) {
    2.     var body : Rigidbody = hit.collider.attachedRigidbody;
    3.     if(gameObject.tag == "Player"){...}
     
  4. voncarp

    voncarp

    Joined:
    Jan 3, 2012
    Posts:
    187
    Generally, triggers need a rigidbody to function properly. I think you need to add either a Rigidbody to your controller or Trigger. I would set it to kinematic to avoid unnecessary physics calculations as well.
     
  5. TKIZZLE

    TKIZZLE

    Joined:
    Dec 2, 2017
    Posts:
    6
    I'm using Unity's built-in FPS Controller so that has the Rigidbody already. I tried adding Rigidbody to the EnterBarCollider in my scene, set to kinematic, and still got nothing.
     
  6. voncarp

    voncarp

    Joined:
    Jan 3, 2012
    Posts:
    187
    Have you tried a Debug.Log("hit") in the trigger to see if its even registering a collision?

    Code (CSharp):
    1. import UnityEngine.UI;
    2. var TextBoxOnCheck : int = 0;
    3. var MessageBox : GameObject;
    4. var TextBox : GameObject;
    5. var TextMessage : String;
    6. function OnTriggerEnter (other : Collider) {
    7. Debug.Log(other.transform.name);
    8.     if(other.gameObject.tag == "Player"){
    9.         TextBoxOnCheck = 1;
    10.         MessageBox.SetActive(true);
    11.         TextBox.GetComponent.<Text>().text = TextMessage;
    12.         TextMessage = "Let's pick our favorite seat at the bar. (Click a stool)";
    13.     }
    14. }
    Also, in your screenshots your messagebox and textbox dont have GameObjects added. If it was a successful trigger, those should return null.
     
    TKIZZLE likes this.
  7. TKIZZLE

    TKIZZLE

    Joined:
    Dec 2, 2017
    Posts:
    6
    I got it. I actually realized myself that I didn't have anything in the MessageBox and TextBox a little bit ago. If it wasn't already clear I'm a rookie... lol. Thank you