• 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
2
Question by burgunfaust · Apr 14, 2011 at 05:06 PM · instantiateprefabdynamicencapsulation

Instantiated prefab click script running on all instances

So I have a prefab that is instantiated multiple times in the game view at runtime. I was trying to set it up so that when a player clicks on that particular object certain information appears.

Problem is that the script is doing that for all the objects of that prefab, rather than just the one object clicked. It is giving the correct information, each has a different name, but it gives me all of them.

var myName : String;

function Update() { if(Input.GetMouseButtonDown(0)) { myName = name; Debug.Log(myName); } }

This script is attached to the prefab object, and therefore spawns as a part of the prefab.

So, what am I doing wrong?

Javascript only please. Thanks in advance!

EDIT: I just want to thank everyone who is trying to help on this again.

Your efforts will not go unnoticed.

Comment
Add comment · Show 18
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 burgunfaust · Apr 17, 2011 at 01:39 PM 0
Share

No one else at all?

avatar image Joshua · Apr 28, 2011 at 01:13 PM 1
Share

Well.. if you cannot get my suggestions (especially method #2) to work, it's definitely you ;)

avatar image Wolfram · Apr 28, 2011 at 01:32 PM 1
Share

Another suggestions: the problem could be that you accidentally instantiated several objects at the same position (for example, creating one each frame while the mouse button is pressed, or something), so several (but not all) instances print the log message. Either way, Update() won't work, either use On$$anonymous$$ouseDown, or Physict.Raycast.

avatar image flaviusxvii · Apr 28, 2011 at 02:39 PM 1
Share

@burgunfaust saying "the script are already attached to the prefab" does NOT $$anonymous$$EAN you've correctly set everything up.

Bottom line, if this works for me, then YOU are having the problem, not the answers.

avatar image superpig ♦♦ · Apr 29, 2011 at 11:52 PM 1
Share

(At least, not as far as the actual Unity engine is concerned. Obviously, the editor maintains prefab relationships so that it knows how to select/revert/apply changes).

Show more comments

6 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by burgunfaust · Apr 30, 2011 at 02:40 AM

So this is not a full answer, but I have found a workaround. My click script would not work unless the transform.z was greater than 10. Very strange. So I moved what needed to be moved and now I can move on. If anyone can shed some light on why, that would be great.

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 Joshua · May 01, 2011 at 06:20 AM 0
Share

Yes. You had a collider overlapping it so your mouse didn't 'reach' the prefab. facepalm.

avatar image burgunfaust · May 01, 2011 at 12:47 PM 0
Share

I suppose that's possible.

  • ‹
  • 1
  • 2

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Instantiate a prefab vs create one dynamically at runtime? 1 Answer

Changing game object based on variable state? 1 Answer

Why is transform.position of a prefab that's been instantiated different than a gameobject created on the scene, when they are in the exact same position? 1 Answer

Instantiating prefab with certain coordinates problem (c#) 0 Answers

Instantiate prefabs just before it comes into view 2 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