I have a problem detecting the collision of objects with each other. I don’t mean player collisions, but objects. The player creates a house and if the house collides with the tree, he has to give gold every 5 seconds. This is the script that is in the cottage object. BoxColliders are added to tree and house objects.
GameObject player;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
print("detec k1");
if (other.gameObject.name == "tree")
{
player.gameObject.GetComponent<Stats>().Odbierz_Gold(10);
print("golda 1");
}
}
void OnCollisionStay2D(Collision2D collision)
{
print("detec k 2");
if (collision.gameObject.name == "tree")
{
player.gameObject.GetComponent<Stats>().Odbierz_Gold(10);
print(" golda 2");
}
Invoke("ReSpawnn", 5);
}
void ReSpawnn()
{
player.gameObject.GetComponent<Stats>().Odbierz_Gold(100);
}