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

Bug Issue with Raycast and Object Activation in Trash Pickup Script

Discussion in 'Scripting' started by GiorgiNinua, Jul 5, 2023.

  1. GiorgiNinua

    GiorgiNinua

    Joined:
    Jul 5, 2023
    Posts:
    10
    Hello community,

    I'm currently working on a trash pickup system for my game, and I'm encountering an issue with the raycast and object activation functionality in my script. I would appreciate any help or insights you can provide to help me resolve this problem.


    I have a Trashpickup script attached to my FPS controller, which is responsible for detecting a key press (E) and performing a raycast from the player's camera position. The goal is to enable a Trash object when the raycast hits it. However, despite verifying the layer setup, collider setup, raycast distance, and camera setup, the object doesn't appear when I press the key.

    What I've tried:

    1. Verified the layer setup: The raycastLayerMask includes the correct layer assigned to the Trash object.
    2. Checked the collider setup: The Trash object has a collider component attached to it and is not marked as a trigger.
    3. Adjusted raycast distance: Increased the raycastDistance value to ensure it covers the distance between the player and the Trash object.
    4. Confirmed camera setup: The correct camera is assigned to the playerCamera variable in the Inspector of the Trashpickup script.
    Code snippet:

    Code (CSharp):
    1. // Relevant code snippet
    2. // ...
    3.  
    4. private void Start()
    5. {
    6.     Trash.SetActive(false);
    7. }
    8.  
    9. void Update()
    10. {
    11.     if (Input.GetKeyUp(KeyCode.E))
    12.     {
    13.         Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition);
    14.         RaycastHit hit;
    15.  
    16.         if (Physics.Raycast(ray, out hit, raycastDistance, raycastLayerMask))
    17.         {
    18.             if (hit.collider.gameObject == Trash)
    19.             {
    20.                 Trash.SetActive(true);
    21.             }
    22.         }
    23.     }
    24. }

    I kindly request your assistance in identifying the possible causes of the issue and helping me find a solution. Is there anything I might be missing or any suggestions you can provide to troubleshoot and resolve this problem?

    Thank you in advance for your support!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    This seems suspect:
    if (hit.collider.gameObject == Trash)

    It seems like you're trying to hardcode/assign in the inspector the trash object? That makes little sense. It assumes there is only one such object in the whole game, and that you already have a direct reference to it. It would be better to simply check the object for a tag or the existence of a component rather than checking for exact equality to a specific GameObject reference.

    Add some Debug.Logs to make sure the code is at least getting to that point and to check which object if any the raycast is actually hitting.
     
    GiorgiNinua likes this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    The script disables the trash object at the start and then later tries to detect it using raycast. The Raycast function can't detect inactive objects.

    Instead disable the mesh renderer in Start then enable the renderer when the Raycast hits it.

    Although this begs the question - how are you supposed to know where the trash is if you can't see it?. :)

    Aren't you supposed to be deleting the trash?.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    Yeah nothing about this makes sense.

    My guess is the OP has some mistaken idea about how prefabs work or something along those lines.
     
  5. GiorgiNinua

    GiorgiNinua

    Joined:
    Jul 5, 2023
    Posts:
    10

    I understand why you were confused. The GameObject named "Trash" that I was referring to is the object held by the player. Through raycasting on another object, it disables that object while enabling an object with the same mesh in the player's hand, thus mimicking an inventory system.
     
  6. GiorgiNinua

    GiorgiNinua

    Joined:
    Jul 5, 2023
    Posts:
    10
    Thank you! Checking the tag did fix the issue.