0
\$\begingroup\$

I have a goal to create a bullet hole in the place where the ray hit, for this purpose I made a material, put a picture of a bullet hole on it, and sent the material itself to plane, and made it a prefab. I created a variable to which I threw this prefab:

public GameObject shootPF;

and I create it in the Instantiate() method:

if (Input.GetMouseButtonDown(0))
{
    Ray ray = _camera.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    if (Physics.Raycast(ray, out hit))
    {
        GameObject hitObject = hit.transform.gameObject;
        EnemyCodeD enemy = hitObject.GetComponent<EnemyCodeD>();
        if (enemy != null)
        {
            enemy.Hit();
        }
        else
        {
            Debug.Log(shootPF);
            Instantiate(
                shootPF,
                hit.point + (hit.normal * .01f),
                Quaternion.FromToRotation(
                Vector3.up,
                hit.normal
            )
        );
    }
}

The problem is that for some reason shootPF is null, although I passed everything in the inspector and there should be no problem with it. Why is that?

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Did you by chance attach the script a second time somewhere and have not set the GO there? \$\endgroup\$
    – Zibelas
    Commented Jun 25 at 8:39
  • \$\begingroup\$ You could try logging the cloned object, like Debug.Log("ShootPF", Instantiate(...)) this should log a message in the console and when you click on it (still while in playmode!) it should select the cloned bullet decal. \$\endgroup\$
    – CheckerT
    Commented Jun 26 at 5:05
  • \$\begingroup\$ @CheckerT Outputs absolutely nothing \$\endgroup\$
    – kaliboba
    Commented Jun 26 at 7:05
  • \$\begingroup\$ So when you click on the message it does not select something or there is no log at all? If there is none at all, it could mean that the else code never executes. Also, turn off error oause in console. \$\endgroup\$
    – CheckerT
    Commented Jun 26 at 10:00

1 Answer 1

1
\$\begingroup\$

You should use logging and debug features to determine what is happening with your code:

if (Input.GetMouseButtonDown(0))
{
    Ray ray = _camera.ScreenPointToRay(Input.mousePosition);

    // Draw the ray in the Scene view in the Unity Editor for 10 seconds.
    Debug.DrawRay(ray.origin, ray.direction, Color.red, 10);

    RaycastHit hit;
    if (Physics.Raycast(ray, out hit))
    {
        GameObject hitObject = hit.transform.gameObject;

        // Log the object that was hit - click the log message in the Unity console to select the object in the Editor
        Debug.Log($"Hit {hitObject.name}", hitObject);

        EnemyCodeD enemy = hitObject.GetComponent<EnemyCodeD>();
        if (enemy != null)
        {
            Debug.Log("Hit object is an enemy");
            enemy.Hit();
        }
        else
        {
            Debug.Log("Hit something that isn't an enemy: " + shootPF);
            Instantiate(
                shootPF,
                hit.point + (hit.normal * .01f),
                Quaternion.FromToRotation(
                Vector3.up,
                hit.normal
            )
        );
    } else {
        Debug.Log("Raycast didn't hit anything");
    }
}

Notice that I've added logging for every possible path that your code can take. From the log messages, you will be able to tell exactly what is happening/going wrong. If the ray isn't hitting anything, look at the line that is drawn in the Scene view to make sure it's going where you expect. If it is, make sure that the object you're trying to click has a collider.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .