Question by
kairgozhins · Jul 11, 2020 at 12:44 PM ·
scripting problemscript.unity adsscriptingproblem
How to use method from one script in the other one? UnityAd script.,How to connect method from other script? After Unity Ad shown giving second chance.
Hello there! I'm struggling with connecting Unity Ads to the button and letting player have second chance (Continue playing) after ad is shown.I have 2 scripts. One has everything related to Menu and UI's, the other is Ad itself. After ad is shown, I want to use the game method StartGame() from 1st script in 2nd script with the ad (at // Reward the user for watching the ad to completion.) That's script 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Advertisements;
using UnityEngine.UI;
public class App_Initialize : MonoBehaviour{
public GameObject inMenuUI;
public GameObject inGameUI;
public GameObject gameOverUI;
public GameObject adButton;
public GameObject player;
private bool hasGameStarted = false;
private bool hasSeenRewardedAd = false;
void Awake() {
Application.targetFrameRate = 60;
}
// Start is called before the first frame update
void Start()
{
player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
inMenuUI.gameObject.SetActive(true);
inGameUI.gameObject.SetActive(false);
gameOverUI.gameObject.SetActive(false);
}
public void PlayButton() {
if (hasGameStarted == true) {
StartCoroutine(StartGame(1.0f));
}
else {
StartCoroutine(StartGame(0.0f));
}
}
public void PauseGame() {
player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
hasGameStarted = true;
inMenuUI.gameObject.SetActive(true);
inGameUI.gameObject.SetActive(false);
gameOverUI.gameObject.SetActive(false);
}
public void GameOver() {
player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
hasGameStarted = true;
inMenuUI.gameObject.SetActive(false);
inGameUI.gameObject.SetActive(false);
gameOverUI.gameObject.SetActive(true);
if (hasSeenRewardedAd == true) {
adButton.GetComponent<Image>().color = new Color (1, 1, 1, 0.5f);
adButton.GetComponent<Button>().enabled = false;
}
}
public void RestartGame() => SceneManager.LoadScene(0); //loads 0 index scene the one in building settings or whatever
public IEnumerator StartGame(float waitTime) {
inMenuUI.gameObject.SetActive(false);
inGameUI.gameObject.SetActive(true);
gameOverUI.gameObject.SetActive(false);
yield return new WaitForSeconds(waitTime);
player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY;
}
}
And this is the second one.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
using System.Collections;
using System.Collections.Generic;
[RequireComponent (typeof (Button))]
public class Ad : MonoBehaviour, IUnityAdsListener {
#if UNITY_IOS
private string gameId = "3706758";
#elif UNITY_ANDROID
private string gameId = "3706759";
#endif
Button myButton;
public string myPlacementId = "rewardedVideo";
void Start () {
myButton = GetComponent <Button> ();
// Set interactivity to be dependent on the Placement’s status:
myButton.interactable = Advertisement.IsReady (myPlacementId);
// Map the ShowRewardedVideo function to the button’s click listener:
if (myButton) myButton.onClick.AddListener (ShowRewardedVideo);
// Initialize the Ads listener and service:
Advertisement.AddListener (this);
Advertisement.Initialize (gameId, true);
}
// Implement a function for showing a rewarded video ad:
void ShowRewardedVideo () {
Advertisement.Show (myPlacementId);
}
// Implement IUnityAdsListener interface methods:
public void OnUnityAdsReady (string placementId) {
// If the ready Placement is rewarded, activate the button:
if (placementId == myPlacementId) {
myButton.interactable = true;
}
}
public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
// Define conditional logic for each ad completion status:
if (showResult == ShowResult.Finished) {
// Reward the user for watching the ad to completion.
} else if (showResult == ShowResult.Skipped) {
// Do not reward the user for skipping the ad.
} else if (showResult == ShowResult.Failed) {
Debug.LogWarning ("Failed");
}
}
public void OnUnityAdsDidError (string message) {
// Log the error.
}
public void OnUnityAdsDidStart (string placementId) {
// Optional actions to take when the end-users triggers an ad.
}
}
Comment