using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FruitGrowth : MonoBehaviour
{
//ParentObject is "Bloom", and the child object under it is "Fruit"
public int RegrowRate = 5;
private int timer = 0;
void update()
{
//fruit = false (collided/Eaten), then timer start, and if time = regrow rate, fruit = true (regrow)
}
public void OnColliOnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name == "Sprite")
{
//then destroy Fruit
Debug.Log(collision.gameObject.name);
}
}
}
2 Answers
2So you need to put a script on the object with the collider e.g.:
public class Bloom : MonoBehaviour
{
// here we assign the object that we want to destroy via editor
public GameObject Fruit;
// then you have some custom method for destroy where you destroy your child object
public void DestroyFruit()
{
Destroy(Fruit);
}
}
Then you modify your OnColliOnCollisionEnter2D method like this:
public void OnColliOnCollisionEnter2D(Collision2D collision)
{
Bloom bloom = collision.gameObject.GetComponent<Bloom>();
// if collision with the object with Bloom script on it
if (bloom != null)
{
//then destroy Fruit
bloom.DestroyFruit();
Debug.Log(collision.gameObject.name);
}
}
got it Thanks man