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 Des6314 · Mar 04, 2016 at 01:54 PM · scripting problemcollisioncollider

Debugging Collision script - Help required to understand behaviour

Hi All.

This is my 1st post so if I use some wrong terms please be patient with me.

I have a simple 3d scene where cubes fall from a spawning location. The cubes can be moved left or right as they fall. There is a Plane underneath the falling cube. When the cube collides with the plane it's movement is stopped and a new cube is spawned. This works perfectly UNTIL a cube falls onto an existing cube, when suddenly the spawner triggers continuously. The spawner script is:

 using UnityEngine;
 using System.Collections;
 
 public class scr_Spawner : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         // Spawn initial Group
         spawnNext();
     }
 
     // Groups
     public GameObject note;
 
     public void spawnNext() {
         Instantiate(note, transform.position, Quaternion.identity);
     }
 }
 

And the collider script is:

 using UnityEngine;
 using System.Collections;
 
 public class scr_Note_Collider : MonoBehaviour {
 
     void OnCollisionEnter(Collision col)
         {
             enabled = false;
             GetComponent<scr_Note_Controller>().enabled = false;
             FindObjectOfType<scr_Spawner> ().spawnNext ();
         }
 
 
     }

Is there something obviously wrong here or is more information needed?

All help gratefully received.

Comment
Add comment · Show 1
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 Exceptione · Mar 04, 2016 at 02:22 PM 0
Share

Not sure if this would fix it but I'd imagine you'd want to disable the scripts AFTER line 10 either way, try that and see if it has an effect?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Matt-Face · Mar 04, 2016 at 03:38 PM

OnCollisionEnter will get called for every new collision. you might want to experiment with OnCollisionStay OR depending on your requirements, you may want to use a bool to check if it has fired yet, so that it only fires on the first collision.

  using UnityEngine;
  using System.Collections;
  
  public class scr_Note_Collider : MonoBehaviour 
 {
      private bool fired = false;
      void OnCollisionEnter(Collision col)
          {
                  if(!fired)
                  {
                          enabled = false;
                          //GetComponent<scr_Note_Controller>().enabled = false;
                          FindObjectOfType<scr_Spawner> ().spawnNext ();
                          fired = true;
                   }
          }
  
  
      }

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

Answer by Des6314 · Mar 09, 2016 at 07:33 AM

Hi @Grin - Sorry for the slow reply and thanks very much for the answer it worked perfectly! Now I just need to understand why :-/

Also, was there a reason that you rem'd out the GetComponent<scr_Note_Controller>().enabled = false;line?

Many thanks again!

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

66 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

Related Questions

How do I make player attack with LMB if the enemy is within range? 0 Answers

How to trigger a BoxCollider with a RigidBody? 0 Answers

Switch Scene on Collision with a Box 1 Answer

How can i restart a script on collision ? 1 Answer

[emergency]Know collision between child object of two object 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