I working with an Admob API from Github -> https://github.com/unity-plugins/Unity-Admob
Im facing a strange problem. My Script works! I start ShowRewardVideo() on button click and it shows me the video. Afterwards i want to gift Coins to my User. As you can see it just increases the current Amount of Coins in PlayerPrefs by adding 20.
Where the problem is? I dont get +20 Coins i geta random amount of coins extra. Last time it was +280 and +320 before that. I tested it with other amounts and it seems like i always get the reward multiple times. So 14 * 20 = 280 and so on.
But why ismy script adding the reward multiple times?
Can you please help me?
private void Start() {
Admob.Instance().rewardedVideoEventHandler += onRewardedVideoEvent;
}
public void ShowRewardVideo() {
if(Admob.Instance().isRewardedVideoReady()) {
Admob.Instance().showRewardedVideo();
} else {
Admob.Instance().loadRewardedVideo("ca-app-pub-5129395190259237/xxxxxxxxx");
ShowRewardVideo();
}
}
void onRewardedVideoEvent(string eventName, string msg) {
Debug.Log("handler onRewardedVideoEvent---" + eventName + " " + msg);
if(eventName == "onRewarded") {
PlayerPrefs.SetInt("mmig", PlayerPrefs.GetInt("mmig") + 20);
PlayerPrefs.Save();
}
}
I would first of all check how often this part of your code gets called:
if(eventName == "onRewarded") {
PlayerPrefs.SetInt("mmig", PlayerPrefs.GetInt("mmig") + 20);
PlayerPrefs.Save();
}
Do this simply make a Debug.Log("Runned") call or something similar.
Also you should maybe consider to do this part:
PlayerPrefs.SetInt("mmig", PlayerPrefs.GetInt("mmig") + 20);
With something like this:
int oldAmount = PlayerPrefs.GetInt("mmig");
int newAmount = oldAmount + 20;
PlayerPrefs.SetInt("mmig",newAmount);
To avoid possible implicit covertig errors. But this change it not necessary for your current problem, just a tip :).
Just make sure to check how many times you get the output above in your console
Related
In the fishing mini game I'm working on, each unique fish that the player catches is written to a "journal" type UI. If the player has caught that type again, instead of writing a whole new entry to the UI, a score increments by 1 to show how many times they've caught that type of fish.
I'm using scriptable objects to store the information about each fish (name, size, a picture, etc) along with the number of times it's been caught. I can see in the inspector that the variable is increasing each time that fish is caught and I'm also debugging a message each time a unique fish has been caught as well as the number of times.
The UI seems to update the number of times caught the first time, however, however it seems to always stay at 1 for every time that fish is caught afterwards, even though the variable that I'm writing to the UI is changing.
Here's the code that updates the UI:
public void UpdateFishPages(Fish fish)
{
if (!CheckIfExists(fish))
{
Debug.Log("New Fish: " + fish.fishName + " " + "caught ");
Debug.Log("Fish index: " + fishIndex);
fishCaught.Add(fish);
fishes[fishIndex].SetActive(true);
fishes[fishIndex].GetComponent<Image>().sprite = fish.image;
fishes[fishIndex].transform.GetChild(0).transform.GetChild(0).GetComponent<TMPro.TextMeshProUGUI>().text = fish.fishName;
fishes[fishIndex].transform.GetChild(0).transform.GetChild(1).GetComponent<TMPro.TextMeshProUGUI>().text = fish.fishDesc;
fishes[fishIndex].transform.GetChild(0).transform.GetChild(2).GetComponent<TMPro.TextMeshProUGUI>().text = fish.fishSize;
string newFishCount = fish.timesCaught.ToString();
Debug.Log(newFishCount);
fishes[fishIndex].transform.GetChild(1).transform.GetChild(0).GetComponent<TMPro.TextMeshProUGUI>().text = newFishCount;
fishIndex++;
}
else
{
string newFishCount = fish.timesCaught.ToString();
Debug.Log(fish.fishName + " now caught " + fish.timesCaught + " times!");
fishes[fishIndex].transform.GetChild(1).transform.GetChild(0).GetComponent<TMPro.TextMeshProUGUI>().text = newFishCount;
}
}
bool CheckIfExists(Fish fish)
{
if (fishCaught.Contains(fish))
{
return true;
}
else
{
return false;
}
}
and here's how I'm calling that method when a player catches the fish:
if (Input.GetKeyDown(KeyCode.E) || Input.GetButtonDown("Button A"))
{
if (timer < 1f)
{
int randomFish = Random.Range(1, 5);
playerAnimator.SetInteger("fishing_randomIndex", randomFish);
fish[randomFish - 1].timesCaught++;
journal.UpdateFishPages(fish[randomFish - 1]);
//Debug.Log("caught");
timer = 0;
fishStage = 2;
}
}
else if (timer > 1f)
{
//Debug.Log("Didn't reel");
playerAnimator.SetTrigger("fishing_fail");
}
I'm hoping someone can shed some light onto what I might be doing wrong.
Many thanks for your time and help!
I have a multiple choice quiz game, and i want to make score. however i want to connect the score with my time renaming therefore if user chooses the right answer he get's 10 points but i also want to multiply that number with the time remaining. so if the time reaches 0 the game ends if he finishes fast he get's higher score, and if he finishes so slow he will lose score. to to summarize i want the time remaining and the points to be connected together.
What i have tried is, on each correct answer i pass in a point and i made it score and also i have a high score, what i can't figure out is how to connect it
to the time remaining?
Here's what i have
public void AnswerButtonClick(bool isCorrect)
{
if (isCorrect)
{
Debug.Log("I'm Correct");
theAnswerIsCorrect = true;
playerScore += currentRoundData.pointAddedForCorrectAnswer;
scoreDisplayText.text = "Score: " + playerScore.ToString();
}
else
theAnswerIsCorrect = false;
// Do we still have questions?
if (questionPool.Length > questionIndex + 1)
{
//questionIndex++;
UpdateQuestionIndex();
StartCoroutine(DelayTime(3));
// ShowQuestion();
}
else
{
EndRound();
}
}
This just add's point if i get the correct answer, what i need is how do also calculate the time remaining with my points.
Here's the time remaining
// Update is called once per frame
void Update ()
{
if (isRoundActive)
{
timerRemaing -= Time.deltaTime;
UpdateTimeRemainingDisplay();
if (timerRemaing <= 0)
EndRound();
}
}
so basically everytime my time is getting lower i lose more score.
Thank you
I think it would be best to start a timer at the beginning of each question, and then create some algorithm for the points. I hope I understood your question right
Why you don't multiply the variable timerRemaing with a Question Score variable and add it to the Player Score variable?
That's what u want right?
I am trying to teach myself C# and Unity knowledge via this course over on Udemy.com.
In one of the first examples a very basic text adventure was created.
Now starting from scratch, I have used the same game structure used there, trying to recreate my own version of this, I've run into a problem:
Input.GetKeyDown(KeyCode.R)
gets activated multiple times in a row and I haven't found the solution in hours.
Here's the relevant parts of the code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TextEngine : MonoBehaviour {
public Text myText;
private int oxygen;
private enum States
{
wakeup, window_0, room_1, corridor_1, corridor_left,
corridor_right, corridor_right_left, locked_supply_door,
damaged_supply_door, supply_door_keypad
}
private States myState;
// Use this for initialization
void Start() {
myState = States.wakeup;
}
// Update is called once per frame
void Update()
{
// Basic gameplay loop:
//if myState equals scenario a, then call method a
if (myState == States.wakeup) wakeup();
if (myState == States.room_1) room1();
if (myState == States.corridor_1) corridor_1();
if (myState == States.corridor_right) corridor_right();
if (myState == States.locked_supply_door) locked_supply_door();
}
void wakeup()
{
oxygen = 100;
myState = States.room_1;
}
void room1()
{
myText.text = "Oxygen Level: " + oxygen + "\n\nYou wake up. Press W to see Window. "
+"Press C to go down the Corridor.";
if (Input.GetKeyDown(KeyCode.W))
{
myState = States.window_0;
}
else if (Input.GetKeyDown(KeyCode.C))
{
myState = States.corridor_1;
}
}
void corridor_1()
{
myText.text = "Oxygen Level: " + oxygen + "\n\nYou walk the corridor to the end."
+ "You can turn Left or Right.\nFrom the Left you hear two gun shots!"
+"\nFrom the Right side comes an eerie silence."
+ "\n\nPress L to turn Left, R to turn Right. Press B to go Back.";
if (Input.GetKeyDown(KeyCode.L))
{
myState = States.corridor_left;
}
else if (Input.GetKeyDown(KeyCode.R))
{
print("R1");
myState = States.corridor_right;
}
else if (Input.GetKeyDown(KeyCode.B))
{
print("B");
myState = States.room_1;
}
}
void corridor_right()
{
myText.text = "Oxygen Level: " + oxygen + "\n\nAnother T-junction!\n"
+ "You can turn Left or Right.\nFrom the left you still hear nothing."
+ "\nFrom the Right side you hear a deep and frightening buzz!"
+ "\n\nPress L to turn Left, R to turn Right. Press B to go Back.";
if (Input.GetKeyDown(KeyCode.L))
{
myState = States.corridor_right_left;
}
else if (Input.GetKeyDown(KeyCode.R))
{
print("R2");
myState = States.locked_supply_door;
}
else if (Input.GetKeyDown(KeyCode.B))
{
print("B");
myState = States.corridor_1;
}
}
void locked_supply_door()
{
myText.text = "Oxygen Level: " + oxygen + "\n\nYou stand in front of a rusty metal door. You step closer. At eye-level there is a dusty sign, "
+ "barely readable. You wipe off the dust. It reads: 'Supply Room'. Maybe you can find some Oxygen tanks and "
+ "some communication devices in there?"
+ "\n\nPress S to Search for a handle."
+ "\n\nPress R to Ram the door, it looks weak!\n\n"
+ "Press B to go Back.";
if (Input.GetKeyDown(KeyCode.S))
{
myState = States.supply_door_keypad;
}
else if (Input.GetKeyDown(KeyCode.R))
{
print("R3");
myState = States.damaged_supply_door;
}
else if (Input.GetKeyDown(KeyCode.B))
{
print("B2");
myState = States.corridor_1;
}
}
}
What I expect to happen:
The game starts.
myState = States.wakeup; gets initalized.
wakeup() is called, myState = States.room_1; calls room1() via Update() and the text displayed on screen.
When pressing the C key, room(1) calls myState = States.corridor_1; which via Update() calls corridor_1() and the text is display again.
Everything working as expected so far. But now everthing goes wrong.
I expect this behavior:
4.A I press the R key, "R1" gets printed and via myState = States.corridor_right; the corridor_right() is called and waits for another key prompt.
But then this happens:
4.B I press the R key, "R1" gets printed and via myState = States.corridor_right; the corridor_right() is called and then without anymore input immediately "R2" gets printed, myState = States.locked_supply_door; is executed, which calls locked_supply_door() via Update() and then immediately "R3" gets printed to the console and then it changes myState = States.damaged_supply_door; where it would continue to call a method via Update(), but I've not yet implemented that method.
The steps seem to get executed correctly, but that GetKeyDown behaves more like `GetKey' and not like itself!
Why does GetKeyDown seemingly get activated multiple times, even when it's supposed to do so only once?
Most problems the people have seem to call GetKeyDowntwice, but not what seems several times.
A common problem seemed to be that the script is attached multiple times somewhere in Unity, which is not the case here: Canvas with Text Field + attaced Script.
If you have made it this far, I want to thank you for the time you already took to help me.
If only I could solve this problem, I cannot find the solution! My rubber duck also doesn't help me here.
The problem is that you are not doing the "update" with ELSE, so when you change from one state to another, it will not wait 1 frame to pass, and will directly go other if, because you changed and is correct, you enter the other if, and because a frame didn't pass till the last R was down, it will still say true to that...
So, in your "Update" function, make them with ELSE IF, not all IF. This way, when you enter one if, when the function inside finished, it will pass 1 frame to re enter.
So change Update to this:
void Update()
{
// Basic gameplay loop:
//if myState equals scenario a, then call method a
if (myState == States.wakeup) wakeup();
else if (myState == States.room_1) room1();
else if (myState == States.corridor_1) corridor_1();
else if (myState == States.corridor_right) corridor_right();
else if (myState == States.locked_supply_door) locked_supply_door();
}
I'm trying to make a game in unity (which uses c#) and what I am trying to accomplish is almost identical to what is done in the game Adventure capitalist. When you click on one of the companies, there is a "cooldown" timer. I put that in quotations because you don't get the money until after the timer has finished. I have looked at the other suggested questions and have managed to create the code below
public UnityEngine.UI.Text showCurrency;
public int money = 0;
public int moneyPerClick = 1;
public float timeToCollect = 3.0F;
private float timeStamp;
private bool buttonClicked;
void Start()
{
timeStamp = Time.time + timeToCollect;
}
void Update()
{
showCurrency.text = "Money: " + money;
if(buttonClicked && timeStamp > 0.0F)
{
timeStamp -= Time.time;
}
if (timeStamp == 0.0F)
{
money += moneyPerClick;
}
}
public bool Clicked()
{
buttonClicked = true;
return buttonClicked;
}
I currently get 1 error but that started happening after I added the showCurrency.text = "Money: " + money; part. So that needs to be fixed.
The code, as far as I can tell, it not working. I don't have the cooldown effect working with the image fill (which will be a problem for another day) So I can't actually see if the timer is counting down, but I guess I could have a Debug.Log and have a system.out line to test that. The other thing that isn't working is I'm not getting the new money amount to show up on screen.
This code is a beginners best guess at how it would be layed out and it is where I'm at. If it looks like I am using the methods wrong, that's probably because I am. Any further information to at least point me in the right direction would be greatly appreciated.
Unity's Update() method gets called every frame. So if you have the game set to 30 FPS, then Update() will get called 30 times every second.
Firstly, timeStamp -= Time.time subtracts the current time from your stored time every single frame (it gets to be a realllly small number really fast). As you have it, try changing your second if statement to an inequality check instead of checking for equality:
if (timeStamp <= 0.0F)
Alternatively, your Update() function could be simplified to something like this:
void Update()
showCurrency.text = "Money: " + getMoney();/*Make money into a property with a getter and setter, so that it will be easily changeable at run-time*/
if(buttonClicked && (Time.time >= timeStamp))
{
timeStamp = (Time.time + timeToCollect);
setMoney(getMoney() + moneyPerClick);
buttonClicked = false; /*Reset your button to be clickable again*/
/*You could also setup your setMoney() like setMoney(moneyPerClick), but that's not what you asked ;b*/
}
}
I'm writing a small mathematical game where sums are generated, and when answered, scored and then another sum is generated. The program is, as of an hour or so ago, flawless but for one thing; the program crashes if the user attempts to input anything that contains more than numbers (with the exception of putting a - before the numbers, for a negative value). Here is my current code for parsing input (I may be using that terminology wrong; if so, I'm sorry):
private void txtAnswer_KeyPress(object sender, KeyPressEventArgs e)
{
// If a press of the enter key is detected...
if(e.KeyChar == (char)Keys.Return)
{
// The player's answer is converted to an integer and checked against the correct answer.
userguess = System.Convert.ToInt32(txtAnswer.Text);
if (userguess == answer)
{
// If the player's answer is correct, an appropriate message is displayed and 1 point added to the kill-score.
lblResult.Text = "# Enemy charge calculated correctly, charge bounced back and damage evaded.";
lblRight.Text = (System.Convert.ToInt32(lblRight.Text) + 1).ToString();
}
else
{
// If the player's answer is incorrect, an appropriate message is displayed and 100 points are added to the damages cost counter.
lblResult.Text = "# Enemy charge calculated incorrectly, charge fired and hit! The charge had " + answer.ToString() + " power units applied.";
lblWrong.Text = (System.Convert.ToInt32(lblWrong.Text) + 100).ToString();
}
// After appropriate action has been taken based on the player's answer, a new sum is generated.
makeNewSum();
}
}
I'm not that advanced of a coder, and I've no idea how I can get it to check what is actually being submitted before allowing it through and crashing if it's bad. Can anyone help me out?
That's because a character for example can't be converted to Int32, change this:
userguess = System.Convert.ToInt32(txtAnswer.Text);
to this:
if (!int.TryParse(txtAnswer.Text, out userguess)) { return; }
If it succeeds then it will set the integer value to userguess so the remainder of the program can stay the same.
Try something like this:
int userguess;
if (int.TryParse(txtAnswer.Text, out userguess))
{
if (userguess == answer)
{
// If the player's answer is correct, an appropriate message is displayed and 1 point added to the kill-score.
lblResult.Text = "# Enemy charge calculated correctly, charge bounced back and damage evaded.";
lblRight.Text = (System.Convert.ToInt32(lblRight.Text) + 1).ToString();
}
else
{
// If the player's answer is incorrect, an appropriate message is displayed and 100 points are added to the damages cost counter.
lblResult.Text = "# Enemy charge calculated incorrectly, charge fired and hit! The charge had " + answer.ToString() + " power units applied.";
lblWrong.Text = (System.Convert.ToInt32(lblWrong.Text) + 100).ToString();
}
// After appropriate action has been taken based on the player's answer, a new sum is generated.
makeNewSum();
}
else{
lblResult.Text = "Incorrect value!";
}
TryParse will attempt to parse your text value and put the result in userguess. If it succeeds it returns true, if it fails it returns false.