Add Delay Time Admob Interstitial on Gameover - c#

I am new in C#, anyone can help me solving this. I am creating game in Unity and have integrated admob (interstitial) in Gameover state. Now it works well, however it looks much annoying for showing insterstitial ads everytime user reach the gameover screen. So here I think it's good to add some delay time within the ads showing. As far I got some simple logic but it doesn't work as I need, not sure.
Here is my code, for example I want it to have 2 minutes delay, so at gameover it won't execute the interstitial ads if it doesn't reach the delay time. The counter should keep counting continously. If the counter reach more 2 minutes and the next gameover screen appear, the ads should show up and the counter restarting counting from zero.
// For timer Admob
public int counter = 0;
public bool timer = true;
void Update()
{
if (timer == true)
{
counter++;
}
}
And in my gameover screen
if (counter >= 120) // delay for 2 minutes
{
<<my custom code to show admob>>
timer = true;
counter = 0;// set the counter to zero and restarting count
}
else
{
timer = true; // it will keep counting
}
Really apreciated for help

I think Update() is called every frame (probably around 30 - 60 times per second) so delay need to be much higher, also to make your game not frame dependant use Time.deltaTime.
http://docs.unity3d.com/ScriptReference/Time-deltaTime.html
Edit: Also in shown code, to timer variable you are never assigning false so looks like it's redundant. You're comparing variable delayAdmob in an if statement, but where do you change or declare it?

Related

IEnumerator WaitForSeconds not working correctly during couroutine

I'm trying to start a couroutine that triggers an animation then waits for 10 seconds and goes into another anim.
I have a simple IEnumerator that goes between 2 anim stages and has a small wait between each one.
When I run the scripts the first time it does wait 10 seconds to start (trapActivate) and then waits 10 seconds to start (trapInactivate) but the animations after that just play one after another without waiting the 10 seconds they should be waiting between anim states.
Not sure what the issue could be.
I looked up a a lot of threads of people having issues because they where starting their couritne inside their Update function but I'm not doing that so not really sure what else could be causing that.
I already tried changing the wait time and no matter how long they are after the first wait they just start playing one after another.
I can see them jumping between each other in the animator
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimateEveryNTime : MonoBehaviour
{
private Animator animator;
public void Start() {
StartCoroutine(playAnimation());
Debug.Log("Couroutine");
animator = GetComponent<Animator>();
}
public IEnumerator playAnimation() {
yield return new WaitForSeconds(10f);
// Insert your Play Animations here
animator.SetTrigger("trapActivate");
yield return new WaitForSeconds(10f);
animator.SetTrigger("trapInactive");
}
}
Here is a pic of my animator layout:
enter image description here
Your image shows the type of the two parameters is Bool not Trigger. The interest thing is SetTrigger can also work for Bool parameters and set their values to true. So you need delete them and create new parameters with the type Trigger and same names.

C# - could someone explain different System.Threading.Thread.Sleep() behaviour on Mac OS X vs Windows 10

I built a terminal Snake application in Visual Studio Community 2017 in Mac OS X.
The game uses this code to wait for user input before moving on to render next frame on terminal:
public static int[] GetUserInput(int[] currentDirection)
{
int[] nextMove = currentDirection; // { x , y }
ConsoleKeyInfo input = new ConsoleKeyInfo();
int timeElapsed = 0;
int waitTime = gameSpeed;
for (int cnt = waitTime; cnt > 0; cnt--)
{
if (Console.KeyAvailable)
{
input = Console.ReadKey();
break;
}
else
{
System.Threading.Thread.Sleep(1);
}
timeElapsed++;
}
// Code that handles input //
System.Threading.Thread.Sleep(waitTime-timeElapsed);
return nextMove;
}
Variable gameSpeed is time in miliseconds during which player must press a key (i.e. rate at which frames are rendered to console).
Basically, user is given certain amount of time to press a key. If a key is pressed, the GetUserInput() function waits remaining time before rendering next frame. If no key is pressed it waits the gameSpeed amount of time before rendering next frame. This way the framerate (i.e. time between frame renderings) of game is kept the same.
When I run this on my old 2011 MacBook Pro the game is perfectly smooth and plays great.
I tried running it on my 2016 Dell Latitude and it doesn't behave even remotely like on my MacBook. The frames render with 1-2 second intervals and if you hold a key down it suddenly starts rendering frames really fast.
This is the only place in the whole application where I use System.Threading.Thread.Sleep(), so there couldn't be other pieces of code which cause this behavior.
I read that using System.Threading.Thread.Sleep() is a bad practice, but I am just curious if someone could explain why the application behaves so differently on these two devices / operating systems.
I am new to programming so any suggestions / pointers to how this code could be written better would be greatly appreciated
Rest of the code can be found on my GitHub repository: https://github.com/krisjanis-a/Snake

Unity:Time.timescale =0 not working

I am trying to display player tutorial when the user 1st starts the game using playerpref and want the game to be paused, the problem i am facing is that Time.timescale=0 is not pausing the game when placed inside start (tutorialCanvas gets displayed), but works when is called by a button(Pause button).
Following is the code I use
void Start()
{
if (PlayerPrefs.HasKey ("test4") ==false ) {
tutorialCanvas.SetActive (true);
Time.timeScale = 0;
}
}
Time.timeScale = 0 is really messed up for me so what i could recommend you is that if you just want to pause something like pausing a movement of a character then you can try it like this :
GameObject PlayerScript;
if(Input.GetKey(KeyCode.P)){
//lets disable the playermovement script only not the whole object
PlayerScript = GetComponent<PlayerMovement>().enabled = false;
}
I've been stucked there also so i made an alternative way. And you can also visit this one. If you want a free of that asset you can get it here
I had this issue recently, although it was only doing it on a build, and not in the editor. Changing timeScale to something like 0.0001f would fix it, but that wasn't a great solution. Creating an input settings asset (under Camera -> Player Input -> Open Input Settings) seemed to have fixed it for the builds.

Track total played time

I am creating game and I need to track some statistics. I want to track total played time (since the user started game and till the moment he definitely ended game).
I have a "continue" option from main menu, so if someone turned game off and then started it and selected "Continue" - it means that I need to increment the old playedTime because, it is not new player.
How can I track this total played time? I cannot use Editor Scripts because I need this to be available just from the game build (player will start game with .exe, not from Unity)
You can store the realtimeSinceStartup on an ApplicationQuit, for example in the playerprefs. Something like this should do the trick:
using UnityEngine;
public class TimeStatistics : MonoBehaviour
{
public float TotalTime
{
get
{
float totalTime = Time.realtimeSinceStartup;
if(PlayerPrefs.HasKey("totaltime"))
totalTime += PlayerPrefs.GetFloat("totaltime");
return totalTime;
}
}
void OnApplicationQuit()
{
PlayerPregs.SetFloat("totaltime", this.TotalTime);
}
}
Make sure the script always exists, otherwise you wont get an OnApplicationQuit!

While loop not working as intended in Pong game

I am attempting to create a Pong game in C# with Visual C# Express 2010.
For the most part, I have the main idea of the game finished, however I have an issue with the ball moving. I have create a for loop, like this:
public void ballSet()
{
if (!Values.isPaused)
{
while(true)
{
if (Values.totalTime.Elapsed.Seconds > 1)
{
Values.totalTime.Restart();
ballMove(50, 50);
}
}
}
}
public void ballMove(int factorX, int factorY)
{
Values.ballLastX = ball.Location.X;
Values.ballLastY = ball.Location.Y;
this.ball.Location = new Point(this.ball.Location.X + factorX, this.ball.Location.Y + factorY);
}
The "ballMove(50, 50);" is just for testing purposes at the moment. The issue is that when ballSet() is called, the form seems to close with a code of 0, meaning that there was no error.
I call ballSet() over here.
public Pong()
{
InitializeComponent();
ballSet();
Values.totalTime.Start();
}
I have already checked and the program does somewhat work when I remove the while loop in ballSet(), as well as the if statement checking the stopwatch (Values.totalTime is a Stopwatch). Obviously since the while loop is commented out, ballMove() is only called once, and the the ball moves once and stops.
Does anyone know how to fix this? I want the ball to be moving constantly, while still having it possible to perform other tasks such as moving the bat in Pong.
This is the output I can give you while running Pong.
http://pastebin.com/nj1pdg3U
From looking at your code, the while loop will never end. I know that's not your reported behaviour, but try swapping ballSet() and Values.totalTime.Start(); around.
Like so:
Values.totalTime.Start();
ballSet();
This is because (in theory) the call to ballSet() will wait for a return, and the totalTime counter will never start, therefore never entering your IF block in the loop.

Categories

Resources