#How can I reward players for completing a level?

1 messages · Page 1 of 1 (latest)

quasi quiver
#

Hi, I am currently making a 2D game and I want it so that after every level I want to have the player be rewarded with 10 coins and I was hoping for help with this.using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelControler : MonoBehaviour
{
public int index;
public int level;
public int coins;
public string Level;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
PlayerCoins playercoins = other.gameObject.GetComponent<PlayerCoins>();
if (playercoins != null)
playercoins.coins += 10;

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1);

}
thats one of my codes
here's the otherusing System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCoins : MonoBehaviour
{
public int coins = 100;
public int level = 1;

public void SavePlayer()
{
    SaveSystem.SavePlayer(this);
}

public void LoadPlayer ()
{
    PlayerData data = SaveSystem.LoadPlayer();
    level = data.level;
    coins = data.coins;
}

}

split charm
#

so what is the problem?

#

it looks like when the player enters the exit area it should give the player 10 coins?

#

but you are making a new playercoins that is not connected to the actual players coins

#

you could try to make a function for the player GainCoins( int coins )

#

instead call that when you enter the exit area

#

this is how i solved kind of similar situation:

#

void OnTriggerEnter(Collider other)
{
// if enemy deal damage
if (other.tag == "enemy")
{
other.GetComponent<Enemy>().takedamage (damage);
}

quasi quiver
split charm
#

yes, well, playercoins class

#

i thought you had player class, but yes it is playercoins

quasi quiver
#

ok thank you

split charm
#

the problem might also be in the scenes

#

if your next scene has new player coins and it is not using the old one from earlier scene

quasi quiver
#

I think this might be the problem, how would I fix it?

split charm
#

it is a structural problem and i am not good with it

#

but i think you should probably create the player coins before you enter the game scene

#

do you have main menu scene or something before you enter the first playable area with exit?

quasi quiver
#

Yes I have it so when the game starts there is an option of either Russian or English and then depending on what you click it will take you to the main menu with the language of your choice and it says tutorial or start. (I only have the tutorial levels done so far)

split charm
#

do you make new playercoins on every scene?

quasi quiver
#

What do you mean?

split charm
#

if you do that, then they have no connection with each others

#

and they always start with 100

quasi quiver
#

What I did is i copy and pasted the "original" character into all of the other scenes

split charm
#

yes, then they are independent different characters

#

they are not the one and same

#

you have to make the playercoins in the menu

#

then you have to somehow call it on your game scene

quasi quiver
#

Well I have one of the characters with the playercoins script in the menu

split charm
#

but you probably make a new one when you enter the game scene?

quasi quiver
#

ok so do you have any ideas on how I can call the script in during the game scene?

split charm
#

i dont remember how you do it in unity