• 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 fulcanmal · Jul 25, 2012 at 02:16 AM · javascriptprefabsonmousedown

Prefabs and OnMouseDown (Javascript)

In my scene, I click on a GUIText to instantiate a tank at a spawn point. The Tank script has an OnMouseDown function which selects the Tank (placing the word "Tank" on the screen via another GUIText). The player starts the game with 1 Tank already. The Tank that is already in the scene works perfectly, but for the instantiated Tank the OnMouseDown function is never called. (I checked using the print command). Just for testing purposes I placed several Tanks in the scene before hitting play and they all work perfectly. OnMouseDown only fails to work for the instantiated Tanks.

The prefab has both a collider and the script attached. I paused the game just to double check that everything was identical between the first Tank and the instantiated version, and it is. Only the names of the objects are different (Tank1 and Tank2). Originally the names were the same, but I changed that hoping it would solve my problem, obviously it did not.

Thank you for your time. I hope someone can help me with this problem.

Edit: I solved the problem, but it creates another issue. A Trigger Collider on an empty, one which I've set up to perform functions that I will add later, is present at the spawnpoint, and was blocking my mouse click. So my new issue is: How can I prevent a Trigger Collider from blocking OnMouseDown for an object inside the Trigger?

Comment
Add comment · Show 9
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 Seth-Bergman · Jul 25, 2012 at 03:24 AM 0
Share

it might help to post the relevant code (such as the On$$anonymous$$ouseDown function)

but my guess is that the tank already in the scene has a variable assigned to it referencing something else in the scene, which the prefab needs to find on awake.. or maybe it isn't tagged right..

did you drag in copies of the prefab to test, or duplicate the tank already in the scene?

avatar image fulcanmal · Jul 25, 2012 at 03:33 AM 0
Share

The issue you're referring to is one that I had earlier, which I fixed in the exact manner you suggested. By setting all the variables using the Start() function. After your suggestion I also tried Awake() just for good measure, no change. None of my code currently uses tags.

avatar image GC1983 · Jul 25, 2012 at 03:42 AM 0
Share

Getting any errors?

avatar image fulcanmal · Jul 25, 2012 at 04:01 AM 0
Share

No errors. It simply doesn't register the On$$anonymous$$ouseDown function.

avatar image GC1983 · Jul 25, 2012 at 04:17 AM 0
Share

The something isnt turned on. isTrigger on the collider?

avatar image Seth-Bergman · Jul 25, 2012 at 04:32 AM 0
Share

how are you detecting the tank in the On$$anonymous$$ouseDown? is there a qualifying condition? (since you're not using tags, and not using the name apparently, I wonder if you're confir$$anonymous$$g the tank somehow...)

avatar image fulcanmal · Jul 25, 2012 at 02:19 PM 0
Share

All the colliders are solid. Everything in the inspector is identical from Tank1 to Tank2. (With the obvious exceptions of name, position, rotation).

Seth Bergman: I'm not sure what you mean. The On$$anonymous$$ouseDown code for the tank enables the GUIText and sets it to "Tank". In any case the first line of code in On$$anonymous$$ouseDown is print("On$$anonymous$$ouseDown Triggered");, and this isn't registering for the instantiated Tank.

avatar image Seth-Bergman · Jul 25, 2012 at 03:16 PM 0
Share

Just checking..

avatar image fulcanmal · Jul 25, 2012 at 04:19 PM 0
Share

Primary issue solved. See edit above.

1 Reply

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

Answer by fulcanmal · Jul 25, 2012 at 06:03 PM

Solved. All I had to do was lower the top of the Trigger Collider. Its always some ridiculously simple solution, isn't 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

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

7 People are following this question.

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

Related Questions

How do I make my textlabel stay after the user clicks on a structure? 1 Answer

Cannot remove a prefab using OnCollisionEnter function. 1 Answer

Need to fix a small gap between two objects, which created by code 1 Answer

Calling a function via button from instance of prefab 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 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