Im having error. I program unity 2D game and there is save system. In game you can collect coins but when player close the game and open it again the score will be the same but coins will be there again. So player can close the game and open it again and gain more coins. I did script Coin which i attached to that coin and there is this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour
{
public int coinValue = 100;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
Player.instance.ChangeScore(coinValue);
}
}
public void destroyCoin()
{
Destroy(gameObject); // this is where i want at the start of the game destroy coin
}
}
Then i have another scirpt atached to player and there is code that move player, change score and there is also code where the player loads score, level and also that code which should at the start check the bool if a coin was picked up in past:
// Start is called before the first frame update
public void Start()
{
//load player
PlayerData data = SaveSystem.LoadPlayer();
level = data.level;
score = data.score;
coinPicked = data.coinPicked;
if (coinPicked == true)
{
Coin.destroyCoin(); // here is that error which i want check if a coin was picked up and if yes then call coin script
}
text.text = score.ToString();
//score manager this
if (instance == null)
{
instance = this;
}
}
//coin pickup 1
public void ChangeScore(int coinValue)
{
score += coinValue;
text.text = score.ToString();
coinPicked = true;
}
//coin pickup 2
public void ChangeScore2(int coinValue2)
{
score += coinValue2;
text.text = score.ToString();
coinPicked2 = true;
}
//take 200 when falling
public void DeleteScore(int deadValue)
{
score += deadValue;
text.text = score.ToString();
}
//save player
public void SavePlayer()
{
SaveSystem.SavePlayer(this);
}
everything works except that delete coin at the start. Unity gives me this error:
Assets\Scripts\Player.cs(98,13): error CS0120: An object reference is required for the non-static field, method, or property 'Coin.destroyCoin()'
Can someone help? Thanks