Search Unity

Box Collider is trigger for GuiText Help!

Discussion in 'Scripting' started by christchild, Jun 14, 2007.

  1. christchild

    christchild

    Joined:
    Jun 12, 2007
    Posts:
    30
    Hey everybody,
    I am making a FP walk around art gallery and I need information to display when you stand in front of each painting.

    So I Created an empty game object,
    gave it a box Collider
    and a GuiText Component.
    And this script:

    function On triggerEnter (col : Collider)
    {
    if (col.name == "Player")
    {

    guiText.text = "Picture Name";
    yeid WaitForSeconds (2);
    guiText.text = " ";

    }
    }

    Nothing happens!
    I am not getting an error message. What am I doing wrong or am I approching
    this the right way?
    Cheers.
     
  2. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    First of all, you need to spell the function "OnTriggerEnter". Hopefully that works.
     
  3. christchild

    christchild

    Joined:
    Jun 12, 2007
    Posts:
    30
    Cheer for the fast reply Moldorma.
    I do have it spelt right in my javascript.
    I typed the javascript into my post instead of copy and paste.

    script should read:

    function OnTriggerEnter (col : Collider)
    {
    if (col.name == "Player")
    {

    guiText.text = "Picture Name";
    yeid WaitForSeconds (2);
    guiText.text = " ";

    }
    }

    Any other ideas?
     
  4. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Sorry to play the spelling police here but what about your use of "yeid" as opposed to "yield"? Just trying to make sure we have all possible complications out of the way (also a reason why it's good to carefully cite your code). This works like a charm for me:

    Code (csharp):
    1.  
    2. var GT : GUIText;
    3.  
    4. function OnTriggerEnter (col : Collider) {
    5.   if (col.name == "Player") {
    6.     GT.text = "Picture Name";
    7.     yield WaitForSeconds (2);
    8.     GT.text = " ";
    9.   }
    10. }
    11.  
    So the only thing that seems a possible problem for you is how you're referencing the GUI Text element, in my case I created a public variable that I then drag-assigned in the UI after attaching the script to an empty GO.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can't really have an object that has both a box collider and a GUIText component. The x/y/z coordinates place the box in 3D space, and also tell the text where to appear on-screen. So just make the box colliders, and have the GUIText object be separate, and reference it like Tom said. I'd also recommend not doing 'guiText.text = "Picture Name"; ', because then you'd presumably need a different script for every object. Probably easiest to name the box colliders the same as you want the text to display, and then get the name from that and display it. Then you'd just need one script and it would work for every object.

    --Eric
     
  6. christchild

    christchild

    Joined:
    Jun 12, 2007
    Posts:
    30
    Thanks you Eric5h5 and HiggyB.
    your both right, I needed to declare a public var to attach a GUIText object.

    HiggyB I used that script you posted, works Great!, Thanks*1000.

    Eric5h5 you are right, I'll try that, it would be sloppy to use a different script for evey Picture.
    My script now looks like this:

    var GT : GUIText;

    function OnTriggerEnter (col : Collider) {
    if (col.name == "Player") {
    GT.text = name;
    yield WaitForSeconds (2);
    GT.text = " ";
    }
    }

    And it displays the name of the Empty object, Again thanks guys.

    XC