I have been trying for about two weeks now to get the IRaycastable interface to work on a script I have created from scratch:
{
public class CropGrower : MonoBehaviour, IRaycastable
{
[SerializeField] PlantConfig crop = null;
[SerializeField] GameObject cropStage = null;
[SerializeField] UnityEvent harvestEvent;
int daysSincePlanted = 0;
public bool isHarvestable = false;
InventoryItem harvestableItem;
bool wasHarvested = false;
public void GrowthStatus(FarmPlot soil)
{
daysSincePlanted++;
print(daysSincePlanted);
if (daysSincePlanted == crop.DaysUntilSeedling())
{
Destroy(transform.GetChild(0).gameObject);
GameObject cropStage = Instantiate(crop.SpawnSeedling(), transform.parent.position, Quaternion.identity);
cropStage.transform.parent = transform;
}
else if (daysSincePlanted == crop.DaysUntilSprout())
{
Destroy(transform.GetChild(0).gameObject);
GameObject cropStage = Instantiate(crop.SpawnSprout(), transform.parent.position, Quaternion.identity);
cropStage.transform.parent = transform;
}
else if (daysSincePlanted == crop.DaysUntilBudding())
{
Destroy(transform.GetChild(0).gameObject);
GameObject cropStage = Instantiate(crop.SpawnBudding(), transform.parent.position, Quaternion.identity);
cropStage.transform.parent = transform;
}
else if (daysSincePlanted == crop.DaysUntilHarvest())
{
Destroy(transform.GetChild(0).gameObject);
GameObject cropStage = Instantiate(crop.SpawnHearvestable(), transform.parent.position, Quaternion.identity);
cropStage.transform.parent = transform;
isHarvestable = true;
harvestableItem = crop.SpawnHarvestedPickup();
print(isHarvestable);
}
}
//private static Ray GetMouseRay()
//{
// return Camera.main.ScreenPointToRay(Input.mousePosition);
//}
//void Update()
//{
// if (Input.GetMouseButtonDown(0))
// {
// HandleRaycast();
// }
//}
//public void HandleRaycast()
//{
// RaycastHit hit;
// if (Physics.Raycast(GetMouseRay(), out hit))
// {
// if (hit.collider.gameObject.tag == "Harvest")
// {
// print(hit);
// CropGrower cropGrower = hit.transform.parent.gameObject.GetComponent<CropGrower>();
// if (ReferenceEquals(cropGrower, cropGrower))
// GetCrop(cropGrower);
// }
// }
//}
//public void GetCrop(CropGrower cropGrower)
//{
// if (!cropGrower.isHarvestable) return;
// if (cropGrower.isHarvestable)
// {
// GameObject player = GameObject.FindGameObjectWithTag("Player");
// bool success = player.GetComponent<Inventory>().AddToFirstEmptySlot(cropGrower.harvestableItem, 1); //adds item to first empty slot
// if (!success)
// {
// player.GetComponent<ItemDropper>().DropItem(cropGrower.harvestableItem, 1); //or drops it on the ground if inventory is full
// }
// Destroy(cropGrower.gameObject);
// cropGrower.GetComponentInParent<FarmPlot>().ResetCropStage();
// }
//}
public CursorType GetCursorType()
{
return CursorType.Harvest;
}
public bool HandleRaycast(PlayerController callingController)
{
if (!isHarvestable) return false;
if (Input.GetMouseButtonDown(0))
{
Debug.Log("hit");
bool success = callingController.GetComponent<Inventory>().AddToFirstEmptySlot(harvestableItem, 1); //adds item to first empty slot
if (!success)
{
GetComponent<ItemDropper>().DropItem(harvestableItem, 1); //or drops it on the ground if inventory is full
}
Destroy(gameObject);
GetComponentInParent<FarmPlot>().ResetCropStage();
}
return true;
}
}
}
The problem is that neither the cursortype nor the handleraycast seem to get called for this component when I press mouse button 0.
I have implemented the interface and provided instructions that are substantially the same as those used in weapon pickup or combat target, but unity just won’t call this HandleRaycast. Why exactly doesn’t this work the way the other components do? Surely this should be able to interact with the PlayerController component the same way all the other components do?