Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Issue with Tags within prefabs

Discussion in 'Scripting' started by Matt_Lacker, Jul 14, 2019.

  1. Matt_Lacker


    May 12, 2019
    I currently have a system in my game where you can look at a certain gameObjects, and if they can be interacted with, a small text popup will appear with the action you can take ("open door", "pickup", etc)

    Currently my systemsworks with an "Interact" script attached to the camera. All gameObjects that can be interacted with have a "HoverTextDisplay" script which holds the text they will display, AND have the tag "Interactable"

    Currently this system works fine for most applications, if I add a cube, tag it, and add the "HoverTextDisplay" component text appears with no issues.

    However, I am running into an issue where a prefab that normally displays text fine no longer works when it is nested within an existing prefab.

    In my case I have an Elevator prefab (main room, doors, floor, and lights) with a nested prefab called "ElevatorPanel" which is the board that would have the floor buttons on a real elevator.

    This prefab consists of a mesh for the panel with two child transforms, which each have a box collider, a HoverTextDisplay component, and are tagged interactable.

    If the ElevatorPanel prefab is placed into the scene by itself the buttons work fine with my interact script. As soon as I place it within the Elevator prefab it stops working.

    If i print the tag of what is being looked at every frame, then Unity claims the object is untagged, but the inspector still shows that they are tagged properly.

    -There is nothing within my other scripts that edits the tag at runtime, and removing it from the prefab makes it work properly.
    - I am fairly certain that nothing is blocking the colliders, moving the panel well outside the elevator (but still part of the same prefab) gives same results

    I feel that this is either a bug, or I am misunderstanding tags/nested prefabs - If anyone could shed some light on this it would be very helpful.

    Thank you-

    Heres is the structure of the prefabs with the tags

    Elevator (untagged)
    ----ElevatorDoor (untagged)
    ----ElevatorDoor1 (untagged)
    ----Carpet (untagged)
    ----Light (untagged)
    ------------ElevatorPanel (nested prefab, untagged)
    --------------------UpButton (interactable)
    --------------------LobbyButton (interactable)

    And here is the script for detecting the text:
    Code (CSharp):
    1.     private void CheckForHoverText()
    2.     {
    3.         Ray ray = new Ray(transform.position, transform.forward);
    4.         RaycastHit hit;
    5.         if (Physics.Raycast(ray, out hit, interactDistance))
    6.         {
    8.              if (hit.transform.CompareTag("Interactable"))
    9.             {
    10.                 print("tag is:" + hit.transform.tag);
    11.                 string hover = hit.collider.gameObject.GetComponent<HoverTextDisplay>().displayText;
    12.                 interactHoverText.OnHover(hover);
    14.             }
  2. GroZZleR


    Feb 1, 2015
    I'm pretty sure RaycastHit.transform returns the transform of the rigidbody, so you're probably getting the root object rather than the collider. Try changing it to hit.collider.transform.

    You can also ditch the tag system entirely and just check if there's a valid component. Might save you some headaches down the road.
    Code (csharp):
    2. if (Physics.Raycast(ray, out hit, interactDistance))
    3. {
    4.    HoverTextDisplay hoverText = hit.collider.gameObject.GetComponent<HoverTextDisplay>();
    6.    if(hoverText != null)
    7.    {
    8.        interactHoverText.OnHover(hoverText.displayText);
    9.    }
    10. }
  3. Matt_Lacker


    May 12, 2019
    That seemed to completely fix the issue - thank you very much!
    GroZZleR likes this.