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!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class scrapMetalPickup : MonoBehaviour {
public int score;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Scrap")
{
score += 10;
Destroy(other.gameObject);
}
}
}
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.
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!
1 Like