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 magnusOP · Oct 01, 2013 at 01:16 AM · raycastprefabtouchdestroyhealth

ray destroying prefab not clone?

i have a game were bubbles fall to the ground i have a raycast script on the bubbles them self but when i try to destroy them it destroys the prefab instead of the clone that are made, if you could help i would really appreciate it very much.

CODE

 function start()
 {
 if (bubbleEasy == true){
  
     Health = 1;
 }
 else if (bubbleMed == true){
  
     Health = 2;
 }     
 else if (bubbleHard == true){
  
     Health = 3;
 }
 }
  
 function Update(){
  
  for (var touch : Touch in Input.touches) 
         {
         
         if (touch.phase == TouchPhase.Began) 
         {
                 
                 // Construct a ray from the current touch coordinates
                 var ray = Camera.main.ScreenPointToRay(touch.position);
                 
                 var hit : RaycastHit;
                 
             
                 if (Physics.Raycast (ray, hit, 400))
                 
                 {
                 Health -= 1;
                 
                 Debug.DrawLine(ray.origin, hit.point);
                 }
             }
         }
                 
  
 if (Health == 0){
  
    PopBubble();   
 }
 }
  
  
  
 function OnMouseDown()
 {
     //Health -= 1;
  
     //Destroy (gameObject);
 }
  
  
 function OnDestroy (){
  
 tapCount += 1;
  
 }
  
 function PopBubble(){
  
 Destroy (gameObject);
  
  
  
  
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by aldonaletto · Oct 01, 2013 at 01:43 AM

You're doing the raycast in all bubbles, and whenever something is hit by the ray all bubbles decrement their Health variables. You could instead use collider.Raycast - this function ignores all other colliders, thus only the bubble hit will be affected:

       ...
       var ray = Camera.main.ScreenPointToRay(touch.position);
       var hit : RaycastHit; 
       if (collider.Raycast (ray, hit, 400))
       {
           Health -= 1;
           Debug.DrawLine(ray.origin, hit.point);
       }
       ...

But it would be way more efficient to do a single raycast in a script attached to the camera and call a function in the bubble script hit that would decrement its Health. The camera script could be like this:

 function Update(){
     for (var touch : Touch in Input.touches) 
     {
         if (touch.phase == TouchPhase.Began) 
         {
             // Construct a ray from the current touch coordinates
             var ray = camera.ScreenPointToRay(touch.position);
             var hit : RaycastHit;
             if (Physics.Raycast (ray, hit, 400))
             {
                 // call function DecHealth(1) in the bubble script:
                 hit.transform.SendMessage("DecHealth", 1, SendMessageOptions.DontRequireReceiver);
             }
         }
     }
 }

The bubble script would be much simpler:

 function Start() // It's Start, not start!
 {
     if (bubbleEasy == true){ 
         Health = 1;
     }
     else if (bubbleMed == true){
         Health = 2;
     }     
     else if (bubbleHard == true){
         Health = 3;
     }
 }
  
 function DecHealth(howMany: int)
 {
     Health -= 1;
     if (Health <= 0){ 
         Destroy(gameObject);
     }
 }
 
 function OnDestroy (){
     tapCount += 1;
 }

NOTE: Start was written in lowercase in your code - it must start with a capital S, otherwise Unity won't call it.

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 magnusOP · Oct 01, 2013 at 03:51 PM

any idea how to limit to just 2 fingers?

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

15 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

Related Questions

How to Destroy multiple object on one hit? 1 Answer

Android touch 3d Object event 1 Answer

Destroying a prefab with a prefab? 2 Answers

Insatiate and Destroy problem 0 Answers

Trouble with raycast hitting a tagged object 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