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 /
  • Help Room /
avatar image
0
Question by wgibbsw · Jun 14, 2016 at 10:48 AM · c#unity 5prefabsinstances

How to interact with multiple instances of a single prefab?

I'm still learning Unity but this has got me stumped and I'm not finding any clues to fix it (mostly because I'm not sure what terms to even search for). I've been trying to make a simple metal detector - the user drags a paddle around the screen and when it nears a prefab called 'treasure' an audio sound beeps faster. So far so good but as soon as I duplicate the treasure prefab only the most recent instance of it works, the older and original ones just stop working.

 public Transform detector; // The metal detector
 public Transform treasure; // The treasure
 public AudioSource beepSound; // The "speaker" that plays the sound
 public AudioClip Beep; // The sound file itself

 void Start() {
     beepSound.PlayOneShot(Beep);

 }

 void Update() {

     float dist = Vector3.Distance(treasure.position, detector.position); // Measure between the treasure and the detector and save it as dist

     if (dist > 0 & dist < 1) { // If less than 1 away from treasure
         if (beepSound.isPlaying == false) { // If the beep isn't currently playing
             beepSound.Play(); // Play the beep    
         }
     }
 }

The treasure prefab has the user's paddle, the audiosource, the beep sound and itself dragged onto it's public variables in the inspector. This has to be a serious newbie question but I'm hoping a prod in the right direction can help me.

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 Mmmpies · Jun 14, 2016 at 11:08 AM 1
Share

There are ways of recording individual prefabs, you could change the name when you instantiate or add a script that holds a variable to identify it but you don't really need that for your example.

What you need it a collider set as a trigger on your prefab. Scale it so when the detector passes through it you call the code using OnCollisionEnter(Collider other).

You want to tick is trigger so the collider doesn't collide it just registers when something goes through it. You can also name or tag your detector so it can tell the difference between your detector and some other gameObject hitting the collider.

 void OnCollisonEnter(Collider other)
 {
     if (other.tag == "Detector")
     {
          // put your beep code here
     }
 }

note that code is for 3D, 2D has it's own colliders.

OnCollisionEnter OnCollisonEnter2D

avatar image wgibbsw Mmmpies · Jun 14, 2016 at 04:42 PM 0
Share

Thanks for the information! I'll get to playing around and see what I could!

1 Reply

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

Answer by EpiFouloux · Jun 14, 2016 at 12:17 PM

The better way to do it is as @Mmmpies says, but if you want to keep it this way :

change public Transform treasure to public Transform[] treasures and fiill it with all the treasure transforms then in your Update() function check the distance to all treasures transforms :

 for (int i = 0; i < treasures.size(); i++)
 {
  // check the distance for treasures[i]
 }
Comment
Add comment · Show 2 · 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 wgibbsw · Jun 14, 2016 at 04:42 PM 0
Share

Cool thanks for the reply, I was ai$$anonymous$$g at keeping it as close to how I'd started but like you guys said it might not be the best way.

avatar image EpiFouloux wgibbsw · Jun 15, 2016 at 07:23 AM 0
Share

Glad we could help :) can you accept my answer so we close the topic?

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

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

Related Questions

Varialbes on prefab script always null 0 Answers

Spawned prefabs are sharing variables values 1 Answer

How can i get a object fall and go up random? 0 Answers

How to catch events from scripted buttons 1 Answer

Trying to move buttons via Coroutine and transform.Translate 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