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 SUPPEAR · Feb 09, 2015 at 01:23 AM · androidmobileobjectsgameobjects

Destroy Object On Touch?

Hello, so I needed a script for destroying an object when it is touched on. This game is 2D and for the android. The last script I used was this -

using UnityEngine; using System.Collections;

public class Break : MonoBehaviour {

 public GameObject cubes;
 
 // Use this for initialization
 void Start () 
 {
     
 }
 
 // Update is called once per frame
 void Update () 
 {
     if (Input.touchCount >0) 
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
         if (Physics.Raycast(ray, out hit))
             if (hit.collider.gameObject.tag =="new")
         {
             Destroy(cubes);
             //cubes.rigidbody.AddForce(Vector3.forward * Time.deltaTime *500);
             
         }        
     }
 }

}

I don't understand why it isn't working. I've changed the tag of the object to new and it still won't work. Any help please?

Comment
Add comment · Show 13
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 incorrect · Feb 09, 2015 at 02:30 AM 0
Share

I bet it's yet another problem with 2D and 3D objects... Are you sure your objects have 3D-colliders? If they have 2D-colliders your raycasting will hit nothing.

avatar image JChiu · Feb 09, 2015 at 02:41 AM 0
Share

Have you look into TouchPhase? Unity needs to know if you want a simple touch on the screen or some sort of movement.

therefore, you should write it as:

if (input.touchCount > 0 && input.GetTouch(0).phase == TouchPhase.Began){ //your codes.....}

avatar image incorrect · Feb 09, 2015 at 02:49 AM 0
Share

@JChiu, by the way, your code will only check if the first touch is just began, ignoring all other touches.

avatar image SUPPEAR · Feb 09, 2015 at 04:56 AM 0
Share

Yes my object does have the 3D box Collider.

avatar image incorrect · Feb 09, 2015 at 05:49 AM 0
Share

Then try this:

 if (Input.touchCount >0) 
 {
     Debug.Log("Touch detected.");
     
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
     if (Physics.Raycast(ray, out hit))
     {
         Debug.Log("Raycast hit " + hit.collider.name);
     
         if (hit.collider.gameObject.tag =="new")
         {
             Debug.Log("The tag matches.");
             Destroy(cubes);
         }        
     }
 }
Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by meat5000 · Feb 09, 2015 at 02:33 AM

http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

2D game you say?

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
avatar image
0

Answer by chariot · Feb 10, 2015 at 08:13 AM

Just if u dont use multitouch :)

 private var mouseEntered : boolean;
 
 function Start () {
 // ur code here
 }
 
 function Update (){
 // ur code here
   if (mouseEntered && Input.GetMouseButtonDown(0)){
     Destroy(gameObject);
   }
 }
 
 function OnMouseEnter(){
   mouseEntered = true;
 }
 
 function OnMouseExit(){
   mouseEntered = false;
 }

This code works fine also for mobile platforms (thx for default converting mouse handler functions by Unity), but only for one-touch apps.

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
avatar image
0

Answer by ASECENAS · Feb 01, 2017 at 09:06 AM

Hi guys, I´m new in this... and I used kind the same code... but I want to measure the time of every touch of an object... it works fine with one... but it doesn´t measure the second touch so it can be destroy in certain time. here is the code:

void Update () {

     int nbTouches = Input.touchCount;

     if (nbTouches > 0)
     {
         print(nbTouches + " touch(es) detected");

         for (int i = 0; i < nbTouches; i++)
         {
             Touch touch = Input.GetTouch(i);
             ray = Camera.main.ScreenPointToRay(touch.position);
             print("Touch index " + touch.fingerId);

             if (Physics.Raycast(ray, out hit, Mathf.Infinity))
             {
                
                     if (hit.collider.tag == "Circle")
                     {

                             touchTime = Time.fixedTime;
                             Debug.Log("TIME " + touchTime);
                             if (touchTime > holdTime)
                             {
                                 touchTime = 0;
                                 Debug.Log("Destroy Object in " + touchTime);
                                 Destroy(hit.collider.gameObject, lifeTime);
                             }
                         
                         
                     }
                 
             }

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

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy Object On Touch? 2D 1 Answer

RigidBody2D Constant Velocity? 3 Answers

Make object move randomly 2D 2 Answers

How can add Android 6.0 on my API Level ( I have all my sdk installed up until Android 7.0 Nougat) 1 Answer

OTA download limits on mobile platforms 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