showing admob interstitial every x play unity - unity3d

I am trying to make my interstitial admob ad appear when a user enters the map for the third time.
My code is:
using UnityEngine;
using System.Collections;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class interstitial_ad : MonoBehaviour {
private InterstitialAd interstitial;
void Start()
{
interstitial = new InterstitialAd ("ca-app-pub-xxx");
interstitial.LoadAd (new AdRequest.Builder ().Build ());
}
void Update()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
}
It shows the ad every time I enter the map, so I tried to add:
playCount++;
if (playCount % 3 == 0) {
but that did not work for me, probably because of the void Update() part. Do you have any ideas/hints how to make it work?

Related

unity ads not showing up

i followed the official unity tutorial on how to put bannner ads on my game, i set everything up in the unity dashboard, and in the editor i see a rectangle that says the ad should be there, but when i build the game nothing shows up, here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
public class bannerad : MonoBehaviour
{
bool testMode = false;
public string gameId = "******" (my game id is here i just dot want to share it);
// Start is called before the first frame update
IEnumerator Start()
{
Advertisement.Initialize(gameId, testMode);
while (!Advertisement.IsReady("Lost_Ad"))
{
yield return null;
}
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
Advertisement.Banner.Show("Lost_Ad");
}
}
anyone knows what is the solution?
Are you ever calling this function? If you just replaced the Start function with this IEnumerator it will not get called unless you call it.
private void Start()
{
Advertisement.Initialize(gameId, testMode);
StartCoroutine(StartAds());
}
private IEnumerator StartAds()
{
// your code here
}

How to network a VR button with PUN in Unity?

I've been working on a solution for weeks now and I think it's time to reach out. I'm trying to make a button that plays a sound when pressed by a controller, everyone will hear that sound. Using VRTK and PlayoVR, I'm able to make a non-networked version where the player can put their hand through a cube, click the trigger from the controller, and it makes a sound.
This is the code for that cube:
namespace VRTK.Examples {
using UnityEngine;
public class Whirlygig : VRTK_InteractableObject
{
public GameObject AudioSource;
public AudioSource LeftSpeaker;
public override void StartUsing(VRTK_InteractUse currentUsingObject =
null)
{
AudioSource.GetComponent<AudioSource>().Play();
}
}
}
Where I get lost is how to network it with Photon Unity Networking. This is what I have:
namespace PlayoVR
{
using UnityEngine;
using VRTK;
using UnityEngine.Video;
using NetBase;
public class PlaySync : Photon.MonoBehaviour
{
public AudioSource LeftSpeaker;
public GameObject Whirlgig;
private bool StartUsing;
// Use this for initialization
void Awake()
{
GetComponent<VRTK_InteractableObject>().InteractableObjectUsed +=
new InteractableObjectEventHandler(DoPlay);
}
void DoPlay(object sender, InteractableObjectEventArgs e)
{
StartUsing = true;
}
// Update is called once per frame
void Update()
{
// Handle firing
if (StartUsing)
{
CmdPlay();
StartUsing = false;
}
}
void CmdPlay()
{
photonView.RPC("NetPlay", PhotonTargets.All);
}
[PunRPC]
void NetPlay()
{
LeftSpeaker.Play();
}
}
}
As you can probably see, I'm a beginner. With this code, when I put my hand in the cube and press the trigger, nothing happens. If anyone can provide any help or even an alternative, I'd be very grateful.
Kind regards,
TheMusiken

Unity Admob Interstitial Ads Not Showing

I know this question has been asked several times but i got stuck even though i have implemented and tried all the solutions. I follow this tutorial for interstitial ads showing:
https://developers.google.com/admob/unity/interstitial
My main goal is to show ad whenever user taps on "Restart" button for the game.
Here is my main ad manager class (which is linked with an game object):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
public class AdManager : MonoBehaviour {
public string interstitial_id;
public string app_id;
public InterstitialAd interstitial;
// Use this for initialization
void Start () {
//MobileAds.Initialize(app_id);
DontDestroyOnLoad(this);
Prepare_Video();
Debug.Log("Admob ilklendirildi: " + interstitial.ToString());
}
public void Show_Video()
{
Debug.Log("Reklam hazırlık durumu: " + interstitial.IsLoaded());
if (interstitial.IsLoaded()) {
Debug.Log("Reklam hazır, gösterilecek");
interstitial.Show();
}
else
{
Prepare_Video();
interstitial.Show();
}
}
public void Destroy_Video()
{
if(interstitial != null)
{
interstitial.Destroy();
}
}
public void Prepare_Video()
{
interstitial = new InterstitialAd(interstitial_id);
AdRequest request = new AdRequest.Builder().Build();
interstitial.LoadAd(request);
}
}
I call the show method in restart action:
public void RestartScene()
{
GameStatusText.gameObject.SetActive(false);
RestartButton.gameObject.SetActive(false);
MeterText.gameObject.SetActive(false);
MeterTextTop.text = "";
Time.timeScale = 1;
TimeController.TimeLeft = 50f;
FindObjectOfType<AdManager>().Show_Video();
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
you need Initialize admob first
Check what's going on with the scene that AdManager is assigned to, and if there is any changes to the scene, and do you have Google Ads unity asset installed in your game?
I have reinstalled the admob plugin and followed the instructions from the beginning. It worked. It seems like my plugin package was damaged and some files were missing.

How to Show Admob Interstitials on Counted Click?

I am trying to something like this:
if button clicked for 4 times, I could show Interstitial?
However i couldn't make it happen.
Created a game object.
Attached script to that object.
Created a restart button.
Implemented this object on that button with onclick-runtime only etc.
Now every time user clicks restart button, interstitial ads show itself. but of course as you might know, it is very annoying.
I dont know how to count how many times users press the restart button and for example show the ads every 3 or 4 times if user press the button.
Thank you.
using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
public class AdScript : MonoBehaviour
{
InterstitialAd interstitial;
public string InterstitialId;
// Use this for initialization
void Start()
{
//Request Ads
//RequestBanner();
RequestInterstitial();
}
public void showInterstitialAd()
{
//Show Ad
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
private void RequestInterstitial()
{
string adUnitId = InterstitialId;
// Initialize an InterstitialAd.
interstitial = new InterstitialAd(adUnitId);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
interstitial.LoadAd(request);
}
}
I can give you idea ,you can use time class to know last showed ad or use variable saying like every 4 game show ad on restart button click.
using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
public class AdScript : MonoBehaviou
{
int counter =0;
InterstitialAd interstitial;
public string InterstitialId;
// Use this for initialization
void Start()
{
//Request Ads
//RequestBanner();
RequestInterstitial();
}
public void showInterstitialAd()
{
counter ++;
if(counter%4==0)
{
//Show Ad
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
}
private void RequestInterstitial()
{
string adUnitId = InterstitialId;
// Initialize an InterstitialAd.
interstitial = new InterstitialAd(adUnitId);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
interstitial.LoadAd(request);
}
}

VRTK_ControllerEvents returning null

I am trying to make a gun in Unity with SteamVR and VRTK but I can't figure out how to properly get controller input. When I use the SteamVR Tracked Controller script I get an IsMatrixValid error and it messes up my HMD. I am currently trying to use VRTK controller events method but it keeps returning as a null even though I followed the video correctly.
Screenshot of script location in project
Video in question: https://www.youtube.com/watch?v=escwjnHFce0
(14:17)
Script in question:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EZEffects;
using VRTK;
public class GunBehavior : MonoBehaviour {
public VRTK_ControllerEvents controllerEvents;
private SteamVR_TrackedObject trackedObj;
public EffectTracer TracerEffect;
public Transform muzzleTransform;
// Use this for initialization
void Start () {
Debug.Log(controllerEvents);
if (controllerEvents)
{
Debug.Log("Hue22");
}
else
{
Debug.Log("work");
}
}
// Update is called once per frame
void Update () {
if (controllerEvents.triggerPressed)
{
ShootWeapon();
}
}
void TriggerPressed(object sender, VRTK.ControllerInteractionEventArgs e)
{
ShootWeapon();
}
void ShootWeapon()
{
RaycastHit hit = new RaycastHit();
Ray ray = new Ray(muzzleTransform.position, muzzleTransform.forward);
TracerEffect.ShowTracerEffect(muzzleTransform.position, muzzleTransform.forward, 250f);
if (Physics.Raycast(ray, out hit, 5000f))
{
Debug.Log("Hueheuhue");
}
}
}
Delete these 3 lines of code from your scrip and it should work
void TriggerPressed(object sender, VRTK.ControllerInteractionEventArgs e)
{
ShootWeapon();
}