Quantcast
Viewing all articles
Browse latest Browse all 1416

Rewarded video ads for two different functions - Unity

I'm implementing reward based video ads in my game. There are two functions that provide the reward. One is a ReceiveLife() function where once the player dies, if they click on the revive button the game restarts and the score is set to score before the player dies instead of 0. The other function is ReceivePoints(), where if the player clicks on the add points button, they are rewarded with 100 extra points. Here are the functions in the game manager script: public class GameManager : MonoBehaviour { public Player player; public Transform startLocation; public GameObject startPanel, ingamePanel, gameoverPanel; private bool gameHasStarted; public enum GameStates { Start, InGame, GameOver } public float playerScore = 0; static int savedScore; private int difficultyLevel = 1; private int maxDiffLevel = 10; private int scoreToNextLevel = 10; private void Start() { SetupNewGame(); } void Update() { if(playerScore >= scoreToNextLevel) { LevelUp(); } if (player.dead == false && gameHasStarted) { playerScore += Time.deltaTime * difficultyLevel; } if (playerScore > PlayerPrefs.GetInt("HighScore", 0)) { PlayerPrefs.SetInt("HighScore", (int)playerScore); } } void LevelUp() { if (difficultyLevel == maxDiffLevel) return; scoreToNextLevel *= 2; difficultyLevel++; } public void SetupNewGame() { playerScore = savedScore; savedScore = 0; player.transform.position = startLocation.position; UpdateUI(GameStates.Start); } public void StartNewGame() { player.dead = false; gameHasStarted = true; playerScore+=savedScore; UpdateUI(GameStates.InGame); } public void GameOver() { player.dead = true; gameHasStarted = false; UpdateUI(GameStates.GameOver); } void UpdateUI(GameStates gameState) { startPanel.SetActive(false); ingamePanel.SetActive(false); gameoverPanel.SetActive(false); if (gameState == GameStates.Start) { startPanel.SetActive(true); } if (gameState == GameStates.InGame) { ingamePanel.SetActive(true); } if (gameState == GameStates.GameOver) { gameoverPanel.SetActive(true); } } public void ReceiveLife() { savedScore = (int)playerScore; SceneManager.LoadScene(1); } public void ReceivePoints() { playerScore+=100; gameOverPanel.gameOverScoreText.text = "Score: " + (int)playerScore).ToString(); } Here is the part of unity ads manager script that provides the reward: public void OnUnityAdsDidFinish(string placementId, ShowResult showResult) { if (showResult == ShowResult.Finished) { gameManager = GameObject.Find("GameManager").GetComponent(); gameManager.ReceiveLife(); } if (showResult == ShowResult.Finished) { gameManager = GameObject.Find("GameManager").GetComponent(); gameManager.ReceivePoints(); } } Once they watch the ad successfully, both the functions are being called. If the player clicks on the ReceiveLife(), he should only get one extra life and not the additional 100 points and vice-versa. I'm not sure how to call each function separately. Do I have to create two separate admanagers in order to call these functions or is there a better method?

Viewing all articles
Browse latest Browse all 1416

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>