Search Unity

Resolved Basic collectible script not working...

Discussion in 'Editor & General Support' started by CatDadJynx, Mar 4, 2021.

  1. CatDadJynx

    CatDadJynx

    Joined:
    Apr 16, 2020
    Posts:
    249
    Hello, cant seem to figure out for the life of me why this isnt working... I have my collectible object (scrap) set to isTrigger and tagged as 'Scrap', with this script attached to my player object, but on collision with the collectible its neither destroyed nor added to my overall score. Any help is appreciated, thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class scrapMetalPickup : MonoBehaviour {
    7.     public int score;
    8.     void OnTriggerEnter(Collider other)
    9.     {
    10.         if (other.tag == "Scrap")
    11.         {
    12.             score += 10;
    13.             Destroy(other.gameObject);
    14.         }
    15.     }
    16. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Make sure you are meeting all the requirements for OnTriggerEnter() to be called (see docs for that method).

    Make sure triggers, rigidbodies, this script, the tag, etc. are all in place.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  3. CatDadJynx

    CatDadJynx

    Joined:
    Apr 16, 2020
    Posts:
    249
    Actually, i figured out the problem, I had my collider attached to the model in my hierarchy but had tagged its parent object (just an empty game object I'm using to hold the model)... when I attached the tag to the actual model instead then it worked. Thanks anyway!
     
    Kurt-Dekker likes this.