So this is a 2D game. The wheat gameObject is an instantiated game object that is instantiated like this. There are 6 of them except with different names.
//code
void InstantiateWheat(){
GameObject Wheat = Instantiate(wheatPrefab, randomPosition, Quaternion.identity);
Wheat.name = "Wheat";
Wheat.gameObject.tag = "Wheat";
Wheat.AddComponent<WheatScript>();
Wheat.GetComponent<SpriteRenderer>().sortingOrder = 1;
}
//By the way, how do I mark this as code?
The script was getting attached and the tag was there, and so was the name, but it still isn’t working. Here’s the main code, in the update function.
if (Input.GetMouseButtonDown(0))
{
Debug.Log("preraycast");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hitInfo = Physics2D.GetRayIntersection(ray);
Debug.Log("predef");
if (hitInfo.collider != null)
{
Debug.Log("pretagcheck");
// Debug.Log(Wheat.name);
if (hitInfo.collider.gameObject.tag == "Wheat")
{
Debug.Log("tagged");
WheatClicked = true;
}
else if (hitInfo.collider.gameObject.tag == "Wheat2")
{
Wheat2Clicked = true;
}
else if (hitInfo.collider.gameObject.tag == "Wheat3")
{
Wheat3Clicked = true;
}
else if (hitInfo.collider.gameObject.tag == "Wheat4")
{
Wheat4Clicked = true;
}
else if (hitInfo.collider.gameObject.tag == "Wheat5")
{
Wheat5Clicked = true;
}
else if (hitInfo.collider.gameObject.tag == "Wheat6")
{
Wheat6Clicked = true;
}
}
}
My goal right now is to make it print tagged, can someone help? Thanks!