I have two different timed Power-Ups in my project, both with 5 seconds duration. I'd like to make that if I pick a Power Up before the time ends, It makes the timer counts again from zero, but it is not happening. If I pick up the same Power UP while remaining 1 second, it lasts just for one second instead of 5. I'm using in the Player Script a public void that is called out by the Power UP when it is picked up
My Player's code:
public void TripleShotActive()
{
_canTripleShot = true;
StopCoroutine(TripleShotPowerDownRoutine());
StartCoroutine(TripleShotPowerDownRoutine());
return;
}
public void SpeedBoostPowerupActive()
{
_speedBoostActive = true;
StopCoroutine(SpeedBoostPowerDownRoutine());
StartCoroutine(SpeedBoostPowerDownRoutine());
return;
}
public void ShieldsActive()
{
_isShieldsActive = true;
_shieldVisualizer.SetActive(true);
}
IEnumerator TripleShotPowerDownRoutine()
{
yield return new WaitForSeconds(5.0f);
_canTripleShot = false;
}
IEnumerator SpeedBoostPowerDownRoutine()
{
yield return new WaitForSeconds(5.0f);
_speedBoostActive = false;
}
I tried using StopCoroutine before StartCoroutine, but it didn't work
You need to stop the coroutine earlier created by StartCoroutine.
Coroutine _tripleShotPowerDown;
public void TripleShotActive()
{
_canTripleShot = true;
if(_tripleShotPowerDown != null)
StopCoroutine(_tripleShotPowerDown);
_tripleShotPowerDown = StartCoroutine(TripleShotPowerDownRoutine());
return;
}
I want my boost work by starting at 0 when the game starts.
In my game I can pick up boost coins to fill up my booster canister.
public static int boost;
private void OnTriggerEnter2D(Collider2D otherObject)
{
if (otherObject.tag == "Boost")
{
boost++;
GetComponent<AudioSource>().PlaySound(BoostSound, 5.7F);
Destroy(otherObject.gameObject);
}
}
After the player picks up enough boost coins to use the booster in the 2d vehicle.
you need a minimum of 3 boost to start booster. Each booster coin is equal to 1 second of boost.
I just want the player to be able to use the booster if they have over 3 boost coins collected, if they have 3 or more than that represents how many seconds they can use the boost for.
Button call code
public void BoostButton()
{
StartCoroutine("Use_VehicleBoost");
}
Booster code that a button calls.
IEnumerator Use_VehicleBoost()
{
// Check which boost package player picked
int boostLevel = SecurePlayerPrefs.GetInt("BoostLevel");
if (boostLevel == 0)
{
Debug.Log("Boost Level: None");
yield return null;
}
else if (boostLevel == 1)
{
Debug.Log("Boost Level: 1");
float aceleRate = 400, maxFWD = -2500;
while (Player.boost >= 3)
{
vehicleController.GetComponent<CarMovement>().accelerationRate += aceleRate;
vehicleController.GetComponent<CarMovement>().maxFwdSpeed += maxFWD;
// Meant to slowly take one point/ Time second away from boost tank
Player.boost = Player.boost - Mathf.RoundToInt(Time.deltaTime);
}
if (Player.boost <= 0)
{
yield return null;
}
}
yield return null;
}
Problem: code stuck inside while loop so Unity freezes. It can never escape the loop but how if Im calling --boost using deltaTime while the loop running.
If you want to use that while loop to check, use it into IEnumerator and wait every frame. Eg:
IEnumerator Use_VehicleBoost(){
...
while (Player.boost >= 3)
{
yield return null;
}
...
}
I've done this by setting a bool in update to flip true when the ability is activated, and flip false once the conditions are no longer met. A method is also called when that bool flips true that holds the desired behavior. I was actually having the same issue as you and didn't realize yield return null was the solution, so this is what I came up with.
if (ultCharge >= pilotUltMin && !pilotUlting && !pilotUltingPressed)
{
if (Input.GetButton(bumperR1))
{
pilotUltingPressed = true;
pilotUlting = true;
curCharge = ultCharge;
pilotUltingPressed = false;
}
}
if (pilotUlting)
{
PilotUlt();
}
}
private void PilotUlt()
{
if (ultCharge > curCharge - pilotUltMax && ultCharge >= 0)
{
rb.AddRelativeForce(Vector2.up * SwitchController.ShipSpeed * 1.5f, ForceMode2D.Impulse);
ultCharge -= 0.7f;
ultExhaust1.SetActive(true);
ultExhaust2.SetActive(true);
}
else
{
pilotUlting = false;
ultExhaust1.SetActive(false);
ultExhaust2.SetActive(false);
}
}
I'm using playerprefs to save data through out scenes. Although I'm having troubles with saving this data when the application is closed. You see I have a IAP shop that gives the player a boomerang when they purchase one, the boomerang effect (done inside my script) is activated through a button. My problem is, is that playerprefs.haskey isn't saving my boomerang effect when I close the game and then reopening it. Although it does save my boomerang effect when through scenes. This is my script:
public bool forceActive = false;
public GameObject BoomerangOn, BoomerangOff;
public static int buttonCount = 0;
static int timesActivated = 0;
void Start()
{
if (PlayerPrefs.HasKey ("boomerangbutton")) {
buttonCount = PlayerPrefs.GetInt ("boomerangbutton");
BoomerangEffect();
}
}
void Update()
{
PlayerPrefs.SetInt("boomerangbutton", buttonCount);
}
public void Activated ()
{
if(timesActivated < BoomeerangText.score)
{
timesActivated++;
StartCoroutine(BoomerangEffect());
}
}
IEnumerator BoomerangEffect()
{
BoomerangOn.SetActive (true);
yield return new WaitForSeconds (10.0f);
BoomerangOn.SetActive (false);
BoomerangOff.SetActive (true);
yield return new WaitForSeconds (1f);
BoomerangOff.SetActive (false);
forceActive = false;
}
Second Edit
Okay I research a bit and linked up boomerang effect script with my boomerang text script. When the user purchase a boomerang from my IAP store, they will get 5 boomerangs, once clicked on, the boomerang text int will go down (like 5, 4, 3, 2 and 1 ) and so will my buttoncount int(that is why the timesactivaed is needed). However I change the Activated function to:
public void Activated ()
{
if (timesActivated < BoomeerangText.score) {
timesActivated++;
StartCoroutine (BoomerangEffect ());
}
}
So far it works regarding activating my boomerang effect when the application is closed, but when it gets to the last int (1) nothing happens, my effect doesn't takes place, so far this is my only problem.
Above is an updated version of what my code looks like now. And below is my Boomerang text script:
public static int score = 0; // The player's score.
public static int click = 1;
public GameObject button;
Text text; // Reference to the Text component.
// Use this for initialization
void Start()
{
if (PlayerPrefs.HasKey ("boomerangTextInt")) {
score = PlayerPrefs.GetInt("boomerangTextInt");
}
}
void Awake()
{
text = GetComponent<Text>();
}
public void Update()
{
SetScoreText();
PlayerPrefs.SetInt("boomerangTextInt", score);
}
void SetScoreText()
{
text.text = " " + score;
if (score <= 0)
{
text.text = "None";
button.GetComponent<Button>().interactable = false;
}
else if (score >= 1)
{
button.GetComponent<Button>().interactable = true;
}
// Set the displayed text to be the word "Score" followed by the score value.
}
public void MinusBoomerangText()
{
score -= click;
text.text = " " + score;
}
}
And in my purchasing script I have this:
public int scoreValue = 5;
if (String.Equals(args.purchasedProduct.definition.id, PRODUCT_5_BOOMERANG, StringComparison.Ordinal))
{
BoomerangEffect.buttonCount += 5;
BoomerangText.score += scoreValue;
Debug.Log("Purchase successfull");
}
Thank you.:)
You are not calling .Save() which means all changes to PlayerPrefs are only in memory and are not persisted to disk, which means the next time you start the application all previous changes are lost.
Try the following in your save function.
void Update()
{
PlayerPrefs.SetInt("boomerangbutton", buttonCount);
PlayerPrefs.Save();
}
Disclaimer : I am not suggesting this is something you should do in your Update at all, as this in inefficient, but this is the root cause of your problem
Alright so I am running into a slight issue, basically, I have a Coroutine that contains a for-loop. I can call the StopCoroutine and StartCoroutine methods successfully, but of course, that just pauses and unpauses the Coroutine. Is there a way to actually stop it and restart it? My end-goal here is to be able to start a Coroutine, kill it at the user's discretion, and restart it from the beginning any time, also at the user's discretion. Is this accomplishable? Thanks.
To "rewind" the coroutine, just call the coroutine method again with the same parameters. If you want to save these parameters, you can use closures:
public class CoroutineClosureExample : MonoBehaviour
{
private System.Func<IEnumerator> CreateCoroutineFactory(int someInt, float someFloat)
{
return () => CoroutineYouWant(someInt, someFloat);
}
private IEnumerator CoroutineYouWant(int someInt, float someFloat)
{
for(int i = 0; i < someInt; i++)
{
yield return new WaitForEndOfFrame();
}
}
private System.Func<IEnumerator> m_CurrentCoroutineFactory;
private IEnumerator m_CurrentCoroutine;
public void SetCoroutineParameters(int someInt, float someFloat)
{
m_CurrentCoroutineFactory = CreateCoroutineFactory(someInt, someFloat);
}
public void RestartCurrentCoroutine()
{
if (m_CurrentCoroutine != null)
{
StopCoroutine(m_CurrentCoroutine);
m_CurrentCoroutine = null;
}
if (m_CurrentCoroutineFactory != null)
{
m_CurrentCoroutine = m_CurrentCoroutineFactory();
StartCoroutine(m_CurrentCoroutine);
}
}
}
I want to make a loading bar in unity 2d game by instantiating 7 cubes every 1 second.
I used :
yield WaitForSeconds(1);
in the function update after every instantiate statement but it didn't work :(( I got an error which is :
Script error : Update() can not be a coroutine.
Any other idea?
I made a new scene and named it "lose" then I wrote this script and attached it to the main camera:
#pragma strict
//var loadingBar: Transform;
var loading_bar : GameObject;
function Update()
{
Instantiate(loadingBar,Vector3(-1.849,-2.9371,2),Quaternion.identity);
gameTimer();
Instantiate(loadingBar,Vector3(-1.2909,-2.937,2),Quaternion.identity);
gameTimer();
Instantiate(loadingBar,Vector3(-0.5566,-2.93711,2),Quaternion.identity);
gameTimer();
Instantiate(loadingBar,Vector3(0.148236,-2.93711,2),Quaternion.identity);
gameTimer();
Instantiate(loadingBar,Vector3(0.823772,-2.93711,2),Quaternion.identity);
gameTimer();
Instantiate(loadingBar,Vector3(1.440567,-2.93711,2),Quaternion.identity);
gameTimer();
Instantiate(loadingBar,Vector3(2.057361,-2.93711,2),Quaternion.identity);
loadingTimer();
Application.LoadLevel(1);
}
function OnGUI()
{
GUI.color = Color.green;
GUI.Label(Rect(400,350,500,500),"<color=green><size=100>Lose</size></color>");
}
function loadingTimer()
{
yield WaitForSeconds(1);
}
I want to these cubes to appear after each other by 1 second so it
will seem like a loading bar ...
I solved this problem by this way ::
#pragma strict
var loadingBar: Transform;
var finished : boolean = false;
function Update()
{
loadingTimer();
if (finished == true)
{
Application.LoadLevel(1);
finished= false;
}
}
function OnGUI()
{
GUI.color = Color.green;
GUI.Label(Rect(295,320,500,500),"<color=green><size=100>Lose</size></color>");
}
function loadingTimer()
{
Instantiate(loadingBar,Vector3(-1.9,-2.9371,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(-1.3,-2.937,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(-1.3,-2.937,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(-0.7,-2.93711,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(-0.1,-2.93711,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(0.5,-2.93711,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(1.1,-2.93711,2),Quaternion.identity);
yield WaitForSeconds(0.28);
Instantiate(loadingBar,Vector3(1.7,-2.93711,2),Quaternion.identity);
finished= true;
}
First of all, You cant use yield WaitForSeconds in Update function. You need to intoduce IEnumator. In your case I can say the following code may help you.
public class Loader : MonoBehaviour
{
public GameObject cube;
private bool finished = false;
private Vector3[] positions = new Vector3[7] {new Vector3(-1.849,-2.9371,2), new Vector3(-1.2909,-2.937,2), new Vector3(-0.5566,-2.93711,2),new Vector3(0.148236,-2.93711,2),new Vector3(0.823772,-2.93711,2),new Vector3(1.440567,-2.93711,2),new Vector3(2.057361,-2.93711,2)};
private int loaderCounter=0;
void Start ()
{
StartCoroutine(StartLoader());
}
IEnumerator StartLoader ()
{
Instantiate(cube,positions[loaderCounter],Quaternion.identity);
yield return new WaitForSeconds(1);
loaderCounter++;
if(loaderCounter==7)
{
finished=true;
}
if(!finished)
{
StartCoroutine(StartLoader());
}
else
{
Application.LoadLevel(1);
}
}
}
Let me know if there is any problem after this. Just use javascript syntax of variable declarations.
Well if you insist want to use Update function, you can. Here is one example how to do it:
private float _elapsedTime = 0;
private int counter = 0;
void Update(){
if(counter < 7){
if(_elapsedTime >= 1){
_elapsedTime = 0; //reset it zero again
_counter++;
//instantiate the cube, and update the loading bar here
}else{
_elapsedTime += Time.deltaTime;
}
}
}
You cannot change the return type of an existing method. Instead you'll want to fire StartCoroutine in your Start or Awake methods and define your IEnumerator as a separate private function of your MonoBehaviour.