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

OnTriggerEnter2D does not respond in my case.

Discussion in 'Scripting' started by Anon1234, Apr 30, 2015.

  1. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CollisionMultiplierOnPlayer : MonoBehaviour {
    5.     void OnTriggerEnter2D(Collider2D other) {
    6.         if (other.gameObject.name == "ENDENGENGENG(Clone)") {
    7.             Debug.Log ("Got the duracel!");
    8.         }
    9.     }
    10. }
    This is script attached to player, and I have all around the place, small green balls. They're all on exactly 0 on 3rd dimension. Both script have attached Rigidbody2D, with enabled "Kinematic", and BoxCollider2D with enabled "isTrigger". The name of prefab is "ENGENGENGENG" and all it's little spawns have appendix "(Clone)" to them, I checked many times.

    There are no errors in console.
     
    Last edited: Apr 30, 2015
  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Well, if the name of your prefab is in fact "ENGENGENGENG", and the spawned items are named "ENGENGENGENG(Clone)", then your problem is nothing more than a simple spelling error. In your script, you're having the player look for contact with "ENDENGENGENG(Clone)" and not "ENGENGENGENG(Clone). Take off the superfluous "D" and give it a try. Also, in the future, I'd suggest that you give your objects far simpler names as doing so will make catching this sort of common error much, much easier for you. That said, I've tested your code and it seems to work fine. Best of luck!
     
    Anon1234 and ThermalFusion like this.
  3. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    Wait, I'm just gonna ask somebody to kick my diamonds, real quick...

    Also, in the future, I'd suggest that you give your objects far simpler names as doing so will make catching this sort of common error much, much easier for you.
    Thank you Sir, I know this already. I haven't slept like 40 hours because I have this temporary project to make. Thanks for help.
     
    krougeau likes this.
  4. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    No problem at all ;) I'm prone to binge programming too, haha, and exhaustion can certainly lead to simple mistakes. Have fun!
     
    Anon1234 likes this.