1
\$\begingroup\$

I'm trying to add a tag check to an if statement but for some reason when I add said tag check it stops working.

void Update ()
    {
        Vector3 fwd = transform.forward; // Equivalent to the code you had.

        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hit;

        if (Input.GetMouseButton(0) && Physics.Raycast(ray, out hit, 10f) && isPlayerHolding == false && gameObject.CompareTag("Pickable"))
        {
                isPlayerHolding = true;
                hit.transform.SetParent(Player.transform);
                Debug.Log("We hit: " + hit.transform.name);
                hit.rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
                hit.rigidbody.constraints = RigidbodyConstraints.FreezePosition;

        }
        if (Input.GetMouseButton(1) && Physics.Raycast(ray, out hit, 10f) && isPlayerHolding == true)
        {
            hit.transform.parent = null;
            isPlayerHolding = false;
            hit.rigidbody.constraints = RigidbodyConstraints.None;
        }

More specifically this is the bit of code that is holding things up "&& gameObject.CompareTag("Pickable")".

Things I've tried:

  • Debugged variables in C# to be sure nothing is missing/null.
  • Making sure the tag is setup properly within Unity.

I really can't think of what the problem is. Anyone have any ideas?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Note that your two back-to-back modifications to hit.rigidbody.contstraints result in the position constraint overwriting the rotation constraint. If you want to freeze both rotation and position (all axes) then you should use a single assignment with RigidbodyConstraints.FreezeAll. You can also use bitwise OR operators (the "pipe" character: |) to combine multiple freeze types for more fine-grained control. \$\endgroup\$ – DMGregory Apr 27 '16 at 23:33
2
\$\begingroup\$

Are you trying to use CompareTag on the gameObject that your RaycastHit is returning? If so it looks like you would need to do "hit.collider.gameObject.CompareTag".

Or if that doesn't work, you would need to move that condition into a nested if statement. So:

if (Input.GetMouseButton(0) && Physics.Raycast(ray, out hit, 10f) && isPlayerHolding == false && hit.collider.gameObject.CompareTag("Pickable"))
    {
            isPlayerHolding = true;
            hit.transform.SetParent(Player.transform);
            Debug.Log("We hit: " + hit.transform.name);
            hit.rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
            hit.rigidbody.constraints = RigidbodyConstraints.FreezePosition;

    }

Or else perhaps:

if (Input.GetMouseButton(0) && Physics.Raycast(ray, out hit, 10f) && isPlayerHolding == false)
    {
          if( hit.collider.gameObject.CompareTag("Pickable")
           {
            isPlayerHolding = true;
            hit.transform.SetParent(Player.transform);
            Debug.Log("We hit: " + hit.transform.name);
            hit.rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
            hit.rigidbody.constraints = RigidbodyConstraints.FreezePosition;
           }
    }

Let me know if I am misunderstanding; it is not immediately clear to me what you're trying to determine has a "Pickable" tag.

\$\endgroup\$
4
  • \$\begingroup\$ Well I tried both methods and both gave the same error citing that RaycastHit does not contain a definition for gameobject. I'm basically trying to determine if the object I've just hit has that tag or not. If it has it continue but if not then don't continue. \$\endgroup\$ – Sie Apr 27 '16 at 19:15
  • \$\begingroup\$ Try "hit.collider.gameObject.CompareTag"? RaycastHit can return colliders and transforms, both of which have gameObject as a variable. \$\endgroup\$ – GorkTi200 Apr 27 '16 at 19:17
  • \$\begingroup\$ That seems to work fine. Thanks. Now I just have to figure out why it is able to go through walls. Even though it isn't kinematic lol. \$\endgroup\$ – Sie Apr 27 '16 at 19:20
  • \$\begingroup\$ I'll update my answer to be more accurate. Glad to help! \$\endgroup\$ – GorkTi200 Apr 27 '16 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.