Scoring system not working?
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;
public class RandomSpawn : MonoBehaviour {
public GameObject Gem;
private int count;
public Text countText;
void Start()
{
count = 0;
SetCountText ();
}
void SpawnGem()
{
Vector3 RandomSpawn = new Vector3 (Random.Range (-8.87F, 8.87F), Random.Range (-3.87F, 3.97F),1F);
Instantiate (Gem, RandomSpawn, Quaternion.identity);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag ("Player"))
{
count = count + 10;
SetCountText ();
SpawnGem ();
Destroy (Gem);
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString ();
}
}
Many thanks
Where is this script ? On the player ? On the gem ? Somewhere else ?
Add a Debug.Log in your OnTriggerEnter2D (before the if statement) to see if the collision is detected.
$$anonymous$$ake sure that either the gem or the player has a Rigidbody.
$$anonymous$$ake sure that the object that collides with the object carrying the script is set as "trigger" in its collider component.
Add a Debug.Log inside the if statement to see if the condition is fulfilled.
@$$anonymous$$dRWaylander , The script is on the gem. The collision is definitely detected because when I run into the gem with the Player gameobject the gem is deleted and a new one spawns in a random position, the player has rigidbody, the gem is set to trigger. After adding a debug to the count variable, I see that it counts to 1 but then no more after more gems are picked up
Answer by Salfii · Apr 27, 2017 at 09:56 AM
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.
Your answer
Follow this Question
Related Questions
Score system not working 1 Answer
Adding Text to Prefab 0 Answers
Why does my text doesn't stay on screen? 1 Answer
Help! with the Message text 0 Answers