Unity ads not working on real device - c#

In my current Unity program I wanted to implement ads. The ads do work in the Unity editor when I run the game, but when I try to run the ads on my iPhone 7 or iPad Air, there are no ads showing up. Does someone know what I am doing wrong?
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Advertisements;
public class GameManager : MonoBehaviour
{
void Start()
{
Advertisement.Initialize("Appstore-id");
}
bool gameHasEnded = false;
public float restartDelay = 1f;
public float addDelay = 1f;
public GameObject completeLevelUI;
public void CompleteLevel ()
{
completeLevelUI.SetActive(true);
Invoke("ShowAdd", addDelay);
}
public void EndGame()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("Game over");
Invoke("Restart", restartDelay);
}
}
public void ShowAdd()
{
if (Advertisement.IsReady())
{
Advertisement.Show ();
}
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}

I had the same problem and the way I fixed it was a recursive function until the ad was ready. Something like:
IEnumerator ShowAd()
{
yield return new waitForSeconds(1);
if (Advertisement.IsReady())
{
Advertisement.Show ();
}
else { StartCoroutine(ShowAd()); }
}
This way it will call the method until the ad is ready to show.
Also, make sure you have test (or debug) mode enabled on Unity Ads Settings.

Just to add another option besides recursively starting an IEnumerator:
IEnumerator ShowAd()
{
while(!Advertisement.isReady())
{
yield return new waitForSeconds(1);
}
Advertisement.Show();
}

Related

in the my code if repeats repeatedly but but it shouldn't be

I press escape, in the debug it says that Pause(), Resum(), Pause() completed. And during the pause, Resum(), Pause(), Resum(), Pause() increase. During the dialogue, I press escape more to turn off the dialogue and still pause. How can I make sure that only one action is performed, and not several different ones? RETURN NOT WORKING
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused;
public DialogWindow dialogWindow;
public GameObject pauseMenuUI;
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Debug.Log("resume");
Resume();
}
if (DialogWindow.IsDialog)
{
Debug.Log("dialog");
dialogWindow.Close();
}
else
{
Debug.Log("pause");
Pause();
}
}
}
public void Resume()
{
Cursor.lockState = CursorLockMode.Locked;
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}
public void Pause()
{
Cursor.lockState = CursorLockMode.None;
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}
public void ToMainMenu(int sceneNumber)
{
SceneManager.LoadScene(sceneNumber);
}
}
Perhaps you could use else if instead of just else, that would eliminate any chance of the different if statements to trigger simultaneously.
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Debug.Log("resume");
Resume();
}
else if (DialogWindow.IsDialog)
{
Debug.Log("dialog");
dialogWindow.Close();
}
else
{
Debug.Log("pause");
Pause();
}
}

Trying to add select button on my controller to open pause menu

So i tryied to add the select button on controller to my script so i can access it with a controller but I don't under stand how because I'm new to coding and if you can explain that would be great.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PauseMenu : MonoBehaviour{
public static bool GameIsPaused = false;
public GameObject pauseMenuUI;
private bool pauseEnabled;
void Update() {
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Resume();
}else
{
Pause();
}
}
}
void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}
void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}
}
First you have to know which button ID's of gamepad/ joystick. I prefer using this package for getting the button ID's of any new gamepad/ joystick. And then by getting the ID's you have to modify the Input Manager if required and call the function, like for example
if(Input.GetKey(KeyCode.Joystick1Button10)){
// do something
}

Everything breaks after using PlayOneShot

In a game I am working on, the player uses a cube to open doors. When the player interacts with the cube while near the door, the door slides open. I am now trying to make it so that while it slides open, it makes a sound as doors should but I kinda ran into some issues. When I tried to add in the sound for the door, it ignored the part where the door should open altogether.
Here is what I did:
I added an AudioSource to the Cube's child object, CORE because the Cube object already contains an AudioSource which will play at the same time as this sound and assigned it in my Cube's script...
public AudioSource Woosh;
void Start()
{
//Debug.Log("Script initialized!");
Hm = gameObject.GetComponent<AudioSource>();
anim = GameObject.Find("TheFirstDoor").GetComponent<Animator>();
//Door = GameObject.Find("TheFirstDoor").GetComponent<AudioSource>();
Woosh = GameObject.Find("CORE").GetComponent<AudioSource>();
}
Here's the interactive part of the script, it runs a check but for some reason, it is pretending CanBeKey is false...
public void OnActivate()
{
if(HasPlayed == false) //Have I played? Has the Time out passed?
{
HasPlayedFirstTime = true;
Hm.Play();
HasPlayed = true;
PlayTimeOut = 30.0f;
if(CanBeKey == true)
{
anim.Play("Door_Open");
//Door.Play();
StartCoroutine(PlayDaWoosh());
Debug.Log("OPENING!");
}
}
}
Now here is the IEnumerator part of the script, PlayDaWoosh()
IEnumerator PlayDaWoosh()
{
Woosh.PlayOneShot(Woosh.clip);
yield return new WaitForSeconds(Woosh.clip.length);
}
I am aware that the last code snippet is a bit messy but it was the best thing I can think of.
Here is the full script in case you are that curious.....
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CUBE_CORE : MonoBehaviour
{
public AudioSource Hm; // HM!!!
private bool HasPlayed = false;
public float PlayTimeOut = 0.0f;
public static bool CanBeKey = false;
public Animator anim;
public static bool HasPlayedFirstTime = false;
public static AudioSource Door;
public AudioSource Woosh;
// Start is called before the first frame update
void Start()
{
//Debug.Log("Script initialized!");
Hm = gameObject.GetComponent<AudioSource>();
anim = GameObject.Find("TheFirstDoor").GetComponent<Animator>();
//Door = GameObject.Find("TheFirstDoor").GetComponent<AudioSource>();
Woosh = GameObject.Find("CORE").GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if(HasPlayed == true)
{
if (PlayTimeOut < 0.0f)
{
HasPlayed = false;
}
PlayTimeOut -= Time.smoothDeltaTime;
}
}
public void OnActivate()
{
if(HasPlayed == false) //Have I played? Has the Time out passed?
{
HasPlayedFirstTime = true;
Hm.Play();
HasPlayed = true;
PlayTimeOut = 30.0f;
if(CanBeKey == true)
{
anim.Play("Door_Open");
//Door.Play();
StartCoroutine(PlayDaWoosh());
Debug.Log("OPENING!");
}
}
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.name == "SecondDoor")
{
anim = GameObject.Find("DoorSecond").GetComponent<Animator>();
}
if(other.gameObject.name == "ThirdDoor")
{
anim = GameObject.Find("TheThirdDoor").GetComponent<Animator>();
}
if(other.gameObject.name == "FourthDoor")
{
anim = GameObject.Find("TheFourthDoor").GetComponent<Animator>();
}
}
IEnumerator PlayDaWoosh()
{
Woosh.PlayOneShot(Woosh.clip);
yield return new WaitForSeconds(Woosh.clip.length);
}
}
Expected result: Door opening and sound playing
Actual result: Neither happening unless I remove anything that has to do with the Woosh
I apologize ahead of time if I wasn't specific enough or if the question was answered somewhere else. I am relatively new here.

Stop coroutine from other script

I am making a game with a maze with audio hints on what way to go. I have the sounds playing on a coroutine and it work in terms of only starting once. However, what I need to do is be able to stop it from another script with a trigger on so that the audio does not keep playing when the player passes a certain point. This is my code so far.
public AudioSource direction;
private bool running = false;
IEnumerator AudioPlay()
{
while (true)
{
direction.Play();
yield return new WaitForSeconds(2);
}
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.CompareTag("Player"))
{
if (running == false)
{
StartCoroutine(AudioPlay());
Debug.Log("Started");
running = true;
}
else if (running == true)
{
Debug.Log("Void");
}
}
}
Use StopCoroutine(previouslyRunCoroutine)
If you use the IEnumerator form (StartCoroutine(AudioPlay());) to start the coroutine, the Unity documentation recommends that you save a reference to the IEnumerator and use that in future calls to StopCoroutine:
public AudioSource direction;
private bool running = false;
public IEnumerator audioPlayCoroutine;
IEnumerator AudioPlay()
{
while (true)
{
direction.Play();
yield return new WaitForSeconds(2);
}
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.CompareTag("Player"))
{
if (running == false)
{
audioPlayCoroutine = AudioPlay();
StartCoroutine(audioPlayCoroutine);
Debug.Log("Started");
running = true;
}
else if (running == true)
{
Debug.Log("Void");
}
}
}
Then, in your other script you can use StopCoroutine:
OtherScript.StopCoroutine(OtherScript.audioPlayCoroutine);
If you were to use the method name form such as StartCoroutine("AudioPlay");, the documentation would recommend using the method name form to stop it:
OtherScript.StopCoroutine("AudioPlay");

Unity animations - yield return new WaitForSeconds(secs) not working

Why doesn't the animation play in Unity when I use this code? How could I make the program wait for the animation to end? In JavaScript (or UnityScript) this approach has worked.
public bool Attacking { get; set; }
private Animator anim;
void Start()
{
anim = gameObject.GetComponent<Animator>();
}
private IEnumerator Attack()
{
Attacking = true;
anim.SetTrigger("Attack"); // this is not playing
yield return new WaitForSeconds(attackLength);
Attacking = false;
}
This is how you run a Coroutine:
StartCoroutine(Attack());
or
StartCoroutine("Attack");
If you try to run it like a usual function it wont work.
So here is solution:
public void PlayAttack()
{
StartCoroutine(Attack());
}
Learn More

Categories