hi there i’ve made a script that when the player walks over a coin it will be picked up and a set amount added to a money count on the screen bronze is 1 silver is 5 and gold is 40 but when i collect any coin 46 is added to the total instead of 1,5 or 40 here is the code what am i doing
wrong
using UnityEngine;
using System.Collections;
public class PickupCoin : MonoBehaviour
{
void OnTriggerEnter (Collider other)
{
if (gameObject.CompareTag("coin_bronze"))
{
Scoremanager.Money += gameObject.GetComponent<CoinValue>().value;
Destroy(other.gameObject);
}
if (gameObject.CompareTag("coin_silver"))
{
Scoremanager.Money +=gameObject.GetComponent<CoinValue>().value;
Destroy(other.gameObject);
}
if (gameObject.CompareTag("coin_gold"))
{
Scoremanager.Money += gameObject.GetComponent<CoinValue>().value;
Destroy(other.gameObject);
}
}
}
ill also add that each coin gold,silver and bronze is tagged appropriately here is the score manager script as well just in case
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Scoremanager : MonoBehaviour {
public static int Money;
Text scorecounter;
void Awake () {
scorecounter =GetComponent <Text>();
Money = 0;
}
void Update () {
scorecounter.text = "Money: " + Money;
}
}
a little update to this question forgot to say that theree is a script on the coins that rotates them ill post that now
using UnityEngine;
using System.Collections;
public class coinspin : MonoBehaviour {
float speed = 0.02f;
GameObject coin;
void Start(){
coin = this.gameObject;
}
void Update(){
coin.transform.Rotate(0,5,0,Space.World);
}
}
ive just edited the question to include an updated vesrion of the pickup script that now uses a script that store the coins value here is said script
using UnityEngine;
using System.Collections;
public class CoinValue : MonoBehaviour {
public int value;
}
Uhm, not trying to marsh on your creativity, but that code seems like a head-ache, and not very dynamic.
My approach would be having a script on the coins that would hold the coins value, and then add the players “money” based on the variable that’s on the coins script.
Your if conditional structure is wrong. It should be:
if (gameObject.CompareTag("coin_bronze"))
{
Destroy(other.gameObject);
Scoremanager.Money += 1;
}
if (gameObject.CompareTag("coin_silver"))
{
Destroy(other.gameObject);
Scoremanager.Money += 5;
}
if (gameObject.CompareTag("coin_gold"))
{
Destroy(other.gameObject);
Scoremanager.Money += 40;
}
You are not using the collider information passed in, so it looks like you are comparing the value to itself, you need to utilize Collider other
in your trigger code.
See Reference: Component.CompareTag
void OnTriggerEnter (Collider other)
{
if(other.CompareTag("coin_bronze"))
{
Scoremanager.Money += gameObject.GetComponent<CoinValue>().value;
Destroy(other.gameObject);
}
else if(other.CompareTag("coin_silver"))
{
Scoremanager.Money +=gameObject.GetComponent<CoinValue>().value;
Destroy(other.gameObject);
}
else if(other.CompareTag("coin_gold"))
{
Scoremanager.Money += gameObject.GetComponent<CoinValue>().value;
Destroy(other.gameObject);
}
}
hey there not sure if this is the right way to do this but i rewrote the scripts and it now works the intended way here are the scripts so others have a basis to work from
adding the following to your coins will make them spin in place until they are collected
using UnityEngine;
using System.Collections;
public class CoinSpin : MonoBehaviour {
float speed = 5f;
void Update(){
transform.Rotate (0,speed,0,Space.World);
}
}
add the following if you want to have multiple different coin types with there own score value
using UnityEngine;
using System.Collections;
public class CoinValue : MonoBehaviour {
public int value;
}
this to collect them
using UnityEngine;
using System.Collections;
public class CoinPickup : MonoBehaviour {
void OnTriggerEnter (Collider col)
{
if (gameObject.CompareTag("coin"))
{
Destroy (gameObject);
ScoreManager.Money += gameObject.GetComponent<CoinValue>().value;
}
}
}
and this will collect info and display it on screen
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreManager : MonoBehaviour {
public static int Money;
Text ScoreCounter;
void Awake () {
ScoreCounter = GetComponent <Text> ();
Money = 0;
}
void Update () {
ScoreCounter.text = "Money: " + Money;
}
}
make a new text Ui element and add that to it to display your score
i hope this helps someone make a nice platform game or mini-game in there project