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 AndreasC · Jul 29, 2014 at 06:30 PM · 2dinstantiateontriggerenter

OnTriggerEnter, called on both instantiated objects

Hey !

I have several objects in my scene, they're all instantiated randomly, and they have the same scripts, because I instantiate them as a prefab. However, I want my OnTriggerEnter2D to only be called on one of the objects, both objects have same tag and layer.

In OnTriggerEnter2D I want to move one of the gameobjects up or down, otherwise it will look weird with two objects inside each other.

What should I do ?

Any answer is appreciated !

/ Andreas

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

Answer by tanoshimi · Jul 29, 2014 at 06:43 PM

OnTrigger messages are always sent to both the object with the trigger collider attached, and also the object that collided with it. If you want the behaviour for rach object to differ you'll have to code that inside the OnTrigger callback somehow (on instantiation of each copy of the prefab, you could assign a variable to determine how it should behave in a collision)

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 weenchehawk · Jul 29, 2014 at 06:55 PM 0
Share

Tanoshimi is right.

Think about it this way . . . .

Which object do you want the OnTriggerEnter to be fired on ? The first one ? (which happens to be exactly the same as the second). Which object knows it's the one to fire, and how does it know ?

Solve this problem and you'll know what to do in your OnTriggerEnter()

avatar image
0

Answer by LostSheepGames · Aug 01, 2014 at 03:51 PM

The system will have to deal with them one at a time, so give your script a boolean flag, something like public bool triggered, and have the OnTriggerEnter do something like this: if (triggered) { triggered = false; return; } Foo foo = other.GetComponent (); foo.triggered = true; //Your movement code here... Where Foo is the name of the script that contains this OnTriggerEnter. This will ensure that only one of the colliding objects deals with the collision.

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

23 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

Related Questions

prefab instantiated by script containing ShadowCaster2D bugs out 0 Answers

Instantiated object won't move! 1 Answer

Instantiate ground/enemies 1 Answer

A null value was found where an object instance was required. 1 Answer

OnMouseDown not firing on prefab instantiated in for loop (2D) 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