Hello,
Each time i click on a spawned fruit and Destroy(gameObject) or Destroy(this.gameObject) all objects are destroyed at the same time. It does not remove the specific object I clicked on. I even set a public gameObject to a reference of what I want destroyed.
Even the sound that is suppose to play, plays on each fruit all at the same time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fruitController : MonoBehaviour {
public GameObject target;
public GameObject fruitToDestroy;
AudioSource popSound;
Vector3 rotation = new Vector3(10, 25, 45);
float step;
public float speed = 15;
// Use this for initialization
void Start () {
popSound = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update ()
{
step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, step * Time.deltaTime);
transform.Rotate(rotation * step * Time.deltaTime);
if(Input.GetMouseButton(0))
{
processFruit(fruitToDestroy);
}
}
public void processFruit(GameObject fruitToDestroy)
{
Debug.Log("Clicked");
Destroy(fruitToDestroy);
popSound.Play();
}
}