- Home /
How to destroy prefab instance on pressing UI button?
I want to destroy coloured dots on button pressed but only when they collide with black dot. So, what i did that i created dots prefabs and spawn the randomly. And i serialised that dot to their respective colour button but this is not working. I tried many work around but nothing is working. @hippocoder
Below is my code:
Dot.cs file which i attached to my dots prefabs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dot : MonoBehaviour
{
public bool canBePressed;
public float speed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Perfect")
{
canBePressed = true;
//Debug.Log(canBePressed);
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Perfect")
{
canBePressed = false;
//Debug.Log(canBePressed);
}
}
}
button.cs file which i attached to my button prefab:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class button : MonoBehaviour
{
[SerializeField] Dot dot;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Kill()
{
if (dot.canBePressed)
{
Destroy(dot.gameObject);
}
}
}
Your answer
Follow this Question
Related Questions
Is there a way to change the sprite for all instances of the same object simultaneously? 1 Answer
Is it possible to have a prefab of a cube, which has instances with different colours? 2 Answers
How to modify a variable on some instances of a prefab in an array of instances? 1 Answer
Changing the Material of an instance of a prefab only! 2 Answers