- Home /
Question by
gpuelston2004 · Mar 15, 2019 at 05:20 PM ·
coinscoingeneral programminggeneral
Is there a better script for permanently deleting items?
So iv been working on a game for awhile now and i'm just getting to making some levels and i have a currency system so i'm making it so the players can collect them threw out the various levels now i have a problem thou as i could just have them respawn in every time i had hoped that they would just delete and be gone so here is the script i used
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class PickupMoney : MonoBehaviour
{
void Start()
{
}
public static class PlayerData
{
private const string playerScoreHash = "score";
public static int Score
{
get
{
return PlayerPrefs.GetInt(playerScoreHash);
}
set
{
PlayerPrefs.SetInt(playerScoreHash, value);
}
}
}
public static class CoinPickup
{
private const string CoinPickupA = "coind";
public static int coind
{
get
{
return PlayerPrefs.GetInt(CoinPickupA);
}
set
{
PlayerPrefs.SetInt(CoinPickupA, value);
}
}
}
public GameObject coin;
void Update()
{
if (CoinPickup.coind == 1)
{
Destroy(coin.gameObject);
}
}
void OnTriggerEnter(Collider obj)
{
if (obj.gameObject.tag == "Player")
{
PlayerData.Score += 5;
CoinPickup.coind = 1;
}
}
}
But if i used this script i will have to make a new player pref each time is there any way i can make this more efficiently
Comment
PlayerPrefs methods are static so you could use it anytime from anywhere i.e. it won't be created it's ready to use.