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. Dismiss Notice

Why doesn't OnCollissionEnter { disable } work?

Discussion in 'Scripting' started by Temp10101, Feb 14, 2015.

  1. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    I've made this code, quite fast and I don't get why it doesn't work:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayableFloorGround : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.        
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.     }
    15.  
    16.     void OnCollisionEnter(Collision collider) {
    17.         if (collider.gameObject.name == "Player") {
    18.             collider.gameObject.SetActive(false);
    19.         }
    20.         Debug.Log(collider);
    21.     }
    22. }
    23.  
    The object is applied to water, the "First Person Controller" is absolutely surely called "Player". But as soon as "Player" jumps into "Water", nothing happens, player just flies through it.
     
  2. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Try it using ontriggerenter else check the RB and colliders
     
  3. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    It sounds like you have set the waters collider as a Trigger, so like tawdry says you most likely want to use OnTriggerEnter as opposed to OnCollisionEnter.
     
  4. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    Changed it, it didn't work. Added rigidbody by default settings (disabled gravity use), both combinations didn't work either.
     
  5. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    I guess I see the problem. You use (Collision collider), while you need to use (Collider collider) when using OnTriggerEnter. Also, you need to use OnTriggerEnter if you set the waters collider as a trigger, and because you said you said "nothing happens, player just flies through it." I assume you have set the waters collsider as a Trigger.
     
    Temp10101 likes this.
  6. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    Ah, here we go. Thank you Sir.
     
  7. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Code (CSharp):
    1.  void OnTriggerEnter(Collider other) {
    2.         if (other.tag == "Player")//player is tag or name? {
    3.            other.collider.gameObject;.SetActive(false);
    4.         }
    5.         Debug.Log(other.name);
    This should work i think
     
  8. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    Someone already provided solving answer. But appreciate your willingness to help.
     
    Last edited: Feb 15, 2015