Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by thegreatdave · Jun 17, 2014 at 05:08 PM · destroycollider2dcollision2d

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");
 
         }
     }
 }
 


Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · Jun 17, 2014 at 10:15 PM 0
Share

I don't quite understand what is happening now and what is wrong with it, and what you would like to happen.

avatar image thegreatdave · Jun 18, 2014 at 07:37 AM 0
Share

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...

avatar image thegreatdave · Jun 18, 2014 at 07:46 AM 0
Share

just like in this gif http://makeagif.com/i/cvnU8F

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges