Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JudyNails · Sep 16, 2011 at 09:52 AM · prefabiphonetouchclonedetect

Detecting touch on cloned prefabs

Hello beautiful people,

I have a prefab with a script attached (see below) which is supposed to detect touch on that object only. When my scene starts for the first time, I instantiate two instances of this prefab. When the player touches that prefab, only the prefab that was touched detects the touch. Excellent. However, if I destroy those two instances and instantiate them again (in my game, I'm now instantiating three instances), all three prefabs detect the touch, rather than the one actually touched.

I would be so grateful if somebody could look at my code and explain what is happening - and how to fix of course ;)

Judy x

 function Update () 
 {
 
     
 
         if( Input.touchCount == 1 )
         {
             
             touch = Input.GetTouch(0);
             
             if (touch.phase == TouchPhase.Began)
             {
                 var ray : Ray = Camera.main.ScreenPointToRay( Input.GetTouch(0).position );
                 var hit: RaycastHit;
                 
                 
                 if( collider.Raycast( ray, hit, 100 ) ) 
                 {
                     
                     Debug.Log ("hit");
                     
                 }
                 
             }
                 
         }
 
     
 
 
 }
Comment
Add comment · Show 2
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 FloatingCoder · Sep 16, 2011 at 09:32 PM 0
Share

Hi Judy, could you post the code where you create the first two, and the second two objects? That might be helpful.

avatar image JudyNails · Sep 17, 2011 at 07:48 AM 0
Share

Thank you, FloatingCoder for taking the time to read this question and ask for more info. In-fact you would have spotted the problem in a second had I posted the complete code! x

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JudyNails · Sep 17, 2011 at 07:47 AM

Well, I'm a complete dummy. The problem was the logic I was using in my code - I forgot to reset a variable back to zero before instantiating the next load of prefabs, so they were being destroyed on the first touch, making it look like a problem with collider.Raycast, which it isn't.

I thought I'd leave this question up because it might be helpful for others who want to detect touch on cloned prefab instances. Most of the touch code on Unity Answers revolves around using Physics.Raycast and detecting the name or the tag of the game object touched. Clearly that's no good when you're instantiating multiple copies of a single prefab.

Simply attach the script in the question above to the prefab and when each prefab is instantiated, it will detect touch and you can insert the appropriate code in place of Debug.Log("hit"); I personally use the Flashbang messenger class to send messages to a 'controller' script that handles all the game logic. If you're not using this messenger class, check it out: http://technology.blurst.com/unityscript-messaging-system/

Hope somebody finds this useful :)

Judy x

Comment
Add comment · Show 1 · 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 memetic-arts · Aug 03, 2012 at 11:01 AM 0
Share

Well, I did indeed find this useful! Though maybe not for the reasons intended . . . I was using a touch event to trigger a set 90 degree rotation on an object (well, discrete rotations on a set of objects), and EVERY time, the object would rotate just a bit more than 90 degrees! Very frustrating to say the least, and was finding no help in the forums . . .

And then I saw your code, and there it was! I'd been using Physics.Raycast(ray, hit, 100) . . . whereas you've got collider.Raycast. And that made the difference!

It would be nice to understand why, at some point, but for now things are behaving and I'm happy.

So thanks again!

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

How to detect Game Object is touched in Unity iPhone? 1 Answer

translating a touchDeltaPosition from the y axis to z axis 1 Answer

Instantiated clones arent behaving the same as the prefab 1 Answer

Holding a Touch down on the iphone 2 Answers

Joystick C# change touch zone? 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