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 Pharaoh_ · Apr 12, 2015 at 12:58 PM · collisionontriggerenter

Unwanted occurence of collision event

Hi,

alt text

The picture above demonstrates what I want to achieve. Let me explain what this is all about and which part does not work as expected.

A, B are two GameObjects that are moving towards the direction of the arrows. Block1, Block2 are also GameObjects, stationary, both having rigidbody and a box collider.

Both A and B have a script attached on them that detects collision. Once collision occurs (OnTriggerEnter()), the object is destroyed.

Upon initialization, the script is disabled for A, because I do not want it to collide with Block1 (this is a temporary solution until I find what's happening).

An IEnumerator moves A and B towards the direction they're supposed to go. Now, although A's script has been disabled, it sometimes collides with Block1, getting itself destroyed. Other times it will go on as intended. If A still had the script enabled, this would make perfect sense. Testing the inspector during runtime, the script is indeed disabled.

Block1, Block2 have no script attached to determine what happens upon collision.

Any ideas on why this weird thing's happening?

unwantedcollision.png (6.1 kB)
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 Digital-Phantom · Apr 12, 2015 at 01:15 PM 0
Share

post scripts for A and B plz

;)

avatar image Pharaoh_ · Apr 12, 2015 at 02:19 PM 0
Share
 public class CollisionEffects : $$anonymous$$onoBehaviour {
     Vector3 initPosition;
 
     void OnTriggerEnter(Collider col) {
         if (col.gameObject.name == "Shield") {
             gameObject.transform.position = initPosition;
             string whichName = gameObject.name;
             string newName = whichName.Substring(5, whichName.Length - 12);
             Transform whichChild = col.gameObject.transform.FindChild (newName+"Particles");
             whichChild.gameObject.SetActive (true);
             gameObject.SetActive (false);
         }
     }
 
     // Use this for initialization
     void Start () {
         initPosition = transform.position;
     }
 
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by equus_ligneus · Apr 12, 2015 at 01:52 PM

Whether a script is enabled or disabled only affects the OnGUI(), Update(), FixedUpdate() and LateUpdate() methods (correct me if I forgot one). Every other callback is executed. You can use a bool to check if your GameObjects should be destroyed or not.

Comment
Add comment · Show 10 · 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 Pharaoh_ · Apr 12, 2015 at 02:20 PM 0
Share

Care to explain why this happens? The script is basically a game object's component. If I set it to active, it will run normally. If it is disabled, it shouldn't. Right?

avatar image meat5000 ♦ · Apr 12, 2015 at 02:44 PM 0
Share

Disabling a script does not remove it from execution. As matey says, Collision routines, custom functions etc will all work even if the script is disabled.

bool check or name/tag check in your collision routine to selectively destroy.

avatar image equus_ligneus · Apr 12, 2015 at 02:58 PM 0
Share

At Pharaoh_: This is not a bug but a design choice. Enabling or disabling a component affects whether callbacks that are called every frame are executed. Other callbacks that are tied to conditions (Awake() to creation, OnTriggerEnter() to entering a trigger) are called so that a script can respond to them. At least that's my interpretation.

avatar image equus_ligneus · Apr 13, 2015 at 07:26 AM 1
Share

It's the same as destroying GameObjects:

 Destroy(unityObjectYouWantToDestroy);

If a script wants to destroy itself it has to call

 Destroy(this);
avatar image equus_ligneus · Apr 14, 2015 at 08:55 PM 1
Share

To fully answer your question: 'this' refers to the instance of a class you are currently in. If you call Destroy(this), only the instance on which Destroy(this) is called will be destroyed.

Show more comments

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

19 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

Related Questions

Trigger Spawning? 1 Answer

OnTriggerEnter and GUIText problem 0 Answers

Destroy object 1 if player walks into object 2 1 Answer

Colliding two GameObjects 1 Answer

Problem with OnTriggerExit 1 Answer


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