I’m trying to make it so that it will increase score by 10 when the gem is picked up by the Player gameObject.
I have the Count: text on screen, but it isnt increasing by 1 when the prefab is picked up.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
I think you need to move the function “OnTriggerEnter2D” to another script and attach it to the player-Gameobject. Then you need to compare if it´s a gem, and not if it´s the player. Your random spawner propably never collides with anything because it´s not attache to the player (hopefully).
if this script is already attached to the pickup or player:
Does the pickup-GameObject and the player GameObject have an collider2D (which one? Edge, Box, etc?) and a rigidBody2D ?
Is “IsTrigger” enabled on the pickup-GO? because you´re using OnTriggerEnter2D so the collider needs to be a trigger. An alternative might be OnCollisionEnter2D()
-To Which gameObject is this script attached? The Player or the pickup? Considering the inner if() of the OnCollisionEnter2D() it might be attached to the pickup but this makes no sense, because the pickup wont spawn itself via SpawnGem()…
Additional hints:
Consider writing an own class/MonoBehaviour for the points or GUI in generall which you call when a pickup is collected.
Don´t use instantiate, google objectPooling or you will have performance problems sooner or later because the GarbageCollector will go nuts than and now.