Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't Check if there are no Collisions

Discussion in 'Scripting' started by _Hybrid, Nov 25, 2016.

  1. _Hybrid

    _Hybrid

    Joined:
    Jul 12, 2016
    Posts:
    5
    Hey, I am really new to Unity and I am trying to make an app.
    All is going well except I need to check if there are any objects colliding with an object I have just floating. The aim of the game is to knock all the blocks off of the place. I have an invisible object with a box collider around all of the blocks. I want the collider to check 'if there are no blocks left on the plane, go to the next scene'. I am still trying to figure out how to do it.
    This is what I have so far: (I am pretty new to this so I'm probably way off)
    Code (CSharp):
    1. void OnCollisionStay(Collision other){
    2.         if (other.rigidbody){
    3.             Debug.Log ("On Plane");
    4.         }
    5.         else
    6.         {
    7.             Debug.Log ("Off Plane");
    8.         }
    9.     }

    If anyone knows how to do it, please let me know.
    (I know how to go to the next scene, I just don't know how to check if there is anything colliding with it)
     
  2. nostalgicbear

    nostalgicbear

    Joined:
    Mar 21, 2013
    Posts:
    98
    If you know that you are going to have objects colliding with the plane for the duration of the game, except when they are all knocked off at the end, you could keep track of the number of collisions via a counter :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class CheckCollisions : MonoBehaviour {
    5.  
    6.     int collisionCounter = 0;
    7.     // Use this for initialization
    8.     void Start () {
    9.        
    10.     }
    11.  
    12.     void OnCollisionEnter(Collision other)
    13.     {
    14.         collisionCounter++;
    15.     }
    16.  
    17.     void OnCollisionExit(Collision other)
    18.     {
    19.         collisionCounter--;
    20.     }
    21. }
    Everytime a collision occurs, the counter goes up. When a collisions ends (knocked off the plane), then the counter goes down. Once the counter reaches 0, you know that youve finished the level.
     
  3. _Hybrid

    _Hybrid

    Joined:
    Jul 12, 2016
    Posts:
    5