-1

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

TONY78
  • 1

1 Answers1

0

The main problem is that Unity does not even know what are you trying to Destroy.

In your coin class you have

public void destroyCoin()
{
Destroy(gameObject);
}

But when you call it from the player you are not destroying the coin, it's like you are trying to destroy the player! That's because you are calling a public function used by the player and it is destroying gameObject which means nothing for Unity from that context.

You can easily fix this by using a function like that

public void destroyCoin(GameObject coin)
{
Destroy(coin);
}

And in your player script since you have the access to Coin component you can access to coin object and then try

Coin.destroyCoin(Coin);

But you can achieve this in many other ways, I don't know why you needed that function because it is useless in your script you can just do this from the Player

Destroy(Coin.gameObject);
Leoverload
  • 1,194
  • 3
  • 8
  • 21
  • I did it and it shows another error... sorry im new to programing:Assets\Scripts\Player.cs(98,30): error CS0103: The name 'coin' does not exist in the current context. Its on player script in: if (coinPicked == true) { Coin.destroyCoin(coin); } – TONY78 Dec 24 '20 at 13:55
  • The thing inside parenthesis must be the coin itself, if you called it "Coin" with capital C then use "Coin" if you called it "coin" use coin. If there is only one coin you can write this: public GameObject coin; and then drag the coin inside it in the inspector and use Destroy(coin); @TONY78 – Leoverload Dec 24 '20 at 13:59
  • Yes i tried chamge the capital letters but with big Coin the error is: 'Coin' is a type, which is not valid in the given context. Wtih small coin its: The name 'coin' does not exist in the current context. – TONY78 Dec 24 '20 at 14:07
  • 1
    @Leoverload the first half of your answer makes no sense ... it is absolutely valid to call `Destroy(gameObject);` which will destroy the object this script is attached to .. so the Coin. The actual issue is quite simple: OP is trying to call the method like if it was static via the `Coin` type .. but it isn't .. it rather has to be called on a certain instance like `coinInstance.destroyCoin();` – derHugo Dec 24 '20 at 14:09