-1
using UnityEngine;

public class Shoot: MonoBehaviour

{

public Camera cam;

void Update()
{
    if (Input.GetButtonDown("Fire1"))
    {
        shooot();
    }
}

void shooot()
{
    RaycastHit hit;
    if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit))
    {
        Debug.Log(hit.transform.name);
    }
}

The other objects have a box collider and Rigid body. The Raycast detects "Cube1" and after I shoot a raycast at something else and then again shoot a raycast at "Cube1" this code does not detect it. Why?

img1

img2

img3

derHugo
  • 83,094
  • 9
  • 75
  • 115
CODER_SCREAM
  • 63
  • 1
  • 1
  • 8
  • 1
    did you try [debugging your code](https://docs.unity3d.com/Manual/ManagedCodeDebugging.html) and see if the method is even executed? If you say it does not detect it ... does it detect something else instead or nothing at all? – derHugo Jul 24 '20 at 07:21
  • I am using Probuilder to build my environment. I think that's the problem. I did try debugging my code. It won't print anything if I shoot a raycast at something which has already been detected. Thanks! – CODER_SCREAM Jul 24 '20 at 07:37
  • No, it is not caused due to probuilder. I checked it! – CODER_SCREAM Jul 24 '20 at 08:21
  • 1
    If the debug shows that nothing is printing, next debug is to add the line debug.log("shooot was called"); just before the function-call for shoot(); as it's possible there's a problem with the inputs? – ArthurEKing Jul 24 '20 at 14:22
  • Could you show the full console output at the end of this test? – Jee Jul 24 '20 at 20:05
  • Also show you're scene view. I think it might have to do with the fact that your are specifying the direction of the ray. – Jee Jul 24 '20 at 20:07
  • THANKS FOR YOUR HELP! It turns out that the console won't log the same name twice(idk why), hence my raycast is working fine, it is detecting an object which has already been detected by it previously, but it is just not logging it!! – CODER_SCREAM Jul 25 '20 at 03:24
  • I had to run a series of test to figure this out. THANKYOU ALL!! – CODER_SCREAM Jul 25 '20 at 03:27
  • Does anybody know why the console won't print the same name twice?? – CODER_SCREAM Jul 25 '20 at 05:28

2 Answers2

1

Press Collapse in the Console of the Unity Editor to toggle wherever prints and logs should be stacked (collapsed) or not, it should still log twice, but instead of appearing as two separate logs the counter on the right increases showing how many times the same equal line have been logged.

Noo Noo
  • 35
  • 1
  • 1
  • 6
1

Unity does log everything. It's just that if you log the same output multiple times Unity collapses into one single line. You can see this by the number indicated on the right side of the output.

(Image taken from https://learn.unity.com/tutorial/introduction-to-the-console-window#5f68b4eeedbc2a002022b83d) enter image description here

If you toggle the marked "Collapse" button, it will show every single output.

QBrute
  • 4,405
  • 6
  • 34
  • 40