- Home /
I have problem destroying my objects
I'm building a game where red jewels(cloned) are falling from above and when they collide in game, i'm calling OnCollisionStay2D method to change the tag name of that objects from "crveni" to "gubavci". And then i call the specific method to destroy all the objects with that tag name in Update() event.
My problem here is when i run the game and they collide and when i click to objects they destroy after some time or i must click on rare point on one jewel in order to destroy the objects with "gubavci" tag name.
Here is my entire script which is attach to red jewel game object. PLEASE HELP!!!
using UnityEngine;
using System.Collections;
using System.Diagnostics;
public class crveniScript : MonoBehaviour {
protected Stopwatch timer = new Stopwatch();
public int Sekunde = 1;
Vector3 startPosition;
GameObject crveniDijamant;
Vector2 mousePosition;
bool delete = false;
bool clicked = false;
// Use this for initialization
void Start () {
timer.Start ();
startPosition = new Vector3(0.1512039f,12.7372004f,0f);
crveniDijamant = GameObject.Find ("crveni");
}
void Update()
{
mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
if (Input.GetMouseButtonDown (0))
{
clicked = true;
UnityEngine.Debug.Log(Input.mousePosition.x.ToString());
}
if(delete)
{
DestroyAllObject("gubavci");
// Destroy(gameObject);
delete = false;
}
}
// Update is called once per frame
void FixedUpdate () {
if (timer.Elapsed.Seconds == Sekunde)
{
GameObject go = Instantiate(crveniDijamant, startPosition, Quaternion.identity) as GameObject;
timer.Reset();
}
}
void OnCollisionStay2D(Collision2D col)
{
if(col.gameObject.tag == "crveni")
{
col.gameObject.tag = "gubavci";
if(clicked)
{
clicked = false;
if (collider2D.OverlapPoint(mousePosition))
{
delete = true;
}
}
}
}
int counter = 0;
void DestroyAllObject(string tag)
{
GameObject[] ObjectsToDestroy = GameObject.FindGameObjectsWithTag(tag);
foreach(GameObject DestroyObject in ObjectsToDestroy)
{
DestroyImmediate(DestroyObject);
counter++;
UnityEngine.Debug.Log("Upravo unistio" + counter.ToString() + "objekata");
}
}
}
I don't quite understand what is happening now and what is wrong with it, and what you would like to happen.
i want when the jewels stay collided and i click with mouse that jewels get destroyed. But funny thing happens they won't destroy every time...
Answer by Dizzyman572 · Jun 18, 2014 at 08:52 AM
I'm not sure if this would work, but try getting rid of clicked = false. You don't really need it and I believe that could be what's messing up. Because I think what could be happening is that your changing a your bool clicked too soon. Therefore, it could be jumping out of your if statement. I would also move col.gameObject.tag = "gubavci"; to line 63. Hope that helps.
Your answer
Follow this Question
Related Questions
OnCollisionEnter is not working for me!! 1 Answer
How to destroy gameobjects with same tag using RaycastHit2D? 1 Answer
Prevent objects from pushing eachother on collide 1 Answer
Destroy Tiles That Collide With Object 1 Answer
Intercept Collider 2D/Collision2D events with a single script on scene 0 Answers