I have been working on a pickup script when you press E. I have put some debug.log’s and the first to if statements are successful but the third debug.log message doesn’t run. I haven’t been able to find the issue… Does anyone know what it may be?
using UnityEngine;
public class IngredientPickUp1 : MonoBehaviour
{
public static bool IngredientOneCollected = false;
void FixedUpdate()
{
RaycastHit hit;
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log("first if successful");
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 10))
{
Debug.Log("second if successful");
if (hit.collider.gameObject.tag == "IngredientOne")
{
Debug.Log("third if succesful");
Destroy(gameObject);
}
}
}
}
}
you have to be sure =) if your object is correctly tag in IngredientOne . You can see you raycast with
Debug.DrawLine(Vector3 origin, Vector3 dest , Color color);
How would I print the object name and tag? I did it awhile back but have forgotten. Would using a layer mask help so that the raycast doesn’t hit the player object?
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 10))
{
// Adding the gameObject parameter at the end of this call will also make the log message clickable to bring you to the object in the hierarchy.
Debug.Log($"Raycast hit object: {hit.transform.name} with tag: {hit.transform.tag}", hit.transform.gameObject);