Pause Button

Hi everyone , i faced a problem and i don’t know how to fix this … i bought a game from assets store and im just beginner in unity … i created a pause button but when i press on this button it pause only the balls like in photo and the circle with baskets still moving ! … and there is the code in the photo… Please guys i need ur help :frowning: and i will appreciate that :slight_smile:


I’m willing to bet the circle-with-baskets are moving through IEnumerator CoRoutine. These tend to run their full cycle despite the time scale being 0. You need to show the source code for the baskets and circle thing. Then I can show you how to fix this problem.

here is the cricle rotation ....

var triggerFallBall : GameObject;
var arrows : Animation;
var rotationalSpeed : float;
var score : GameObject;
var time : float = 2;
var boost : int = 500;
private var empty : boolean = true;
function Update () {
//ticking
time -= 1 * Time.deltaTime;
//if the time is over
if(time <= 0){
//spin the wheel
gameObject.transform.Rotate(0,0,rotationalSpeed);
}
if(score.GetComponent(Scores).score >= boost){
rotationalSpeed += 0.05;
arrows.Play();
boost += 500;
}

if(triggerFallBall.GetComponent(FallBall).numberOfBalls1 == 9 && rotationalSpeed > 0){
if(empty == true){
if(PlayerPrefs.GetInt("SoundBoolean") == 0){
//play sounds when it is switched on.
triggerFallBall.GetComponent.<AudioSource>().Play();
}
empty = false;
}
rotationalSpeed -= 0.01;
}
if(triggerFallBall.GetComponent(FallBall).numberOfBalls2 == 9 && rotationalSpeed > 0){
if(empty == true){
if(PlayerPrefs.GetInt("SoundBoolean") == 0){
//play sounds when it is switched on.
triggerFallBall.GetComponent.<AudioSource>().Play();
}
empty = false;
}
rotationalSpeed -= 0.01;
}
}

Ooohhhh… Looks like we need to recode the whole script or translate it from JavaScript to C#. I don’t think I have the time to do this tonight. What version of Unity are you using? I’d hate to draw up some code that won’t work with your version. This looks like around Unity 5 or earlier. Can’t remember the last time I seen JavaScript. I can easily translate this though, no worries. The main issue would be can your version handle the code?

So anyway, lemme give you an example of how you can prevent the things from rotating on Pause.

if(time <= 0){
//spin the wheel

// Here you will want to make a public static bool variable
// on your Pause script name it isPaused
// When you pause the game, set the variable to true;
// When unpaused set the variable to false;

if(GameManager.isPaused == false)
{
gameObject.transform.Rotate(0,0,rotationalSpeed);
}
}

So now the wheel shouldn’t be able to spin when you pause the game.

im using unity 5.6.7.f1 , but i don’t know anything in javascript … i don’t know how to do it

It’s really not that much different, to be honest. However, you don’t need to code JavaScript for this to work.

In your GameManager script you want to create a static variable.

public class GameManager : MonoBehavior
{
public static bool isPaused;

void Start()
{
isPaused = false;
}

// Inside of your function (Didn't feel like typing it out)

if(paused)
{
pause = true;
isPaused = true;
}
else{ pause = false; isPaused = false; }

}

And then you should be able to still use the code I shown you last night.

You’re using a really old an outdated version of Unity. What made you choose this particular version?

I’m just curious, where are you calling your PauseControl function?

5212601--519059--upload_2019-11-25_15-54-43.png

Kinda seems like you should have it in your Update()

void Update()
{
PauseControl();
}

Or is this on a UI button OnClick event?

yes , its UI button OnClick event …

but i still don’t know how to fix … could you rewrite with fix the script which i made , im very sorry bro …

To rewrite your script, I would also need you Scores script because there’s a reference to it. Then I’d have to rewrite that script as well. But to translate the code, it would look much like this: (Note- I’m stopping when I run into Scores)

private Animation arrows;
private GameObject triggerFallBall;
private GameObject score;
private float roatationalSpeed;
private float time = 2f;
private int boost = 500;
private bool empty = true;

private void Start()
{
arrows = GetComponent<Animation>();

// now here is where my confusion begins...
// How to properly grab your game objects?
// I don't know if your scripts are attached or not ...
// If yes then this:
triggerFallBall = GetComponent<GameObject>();
score = GetComponent<GameObject>();

// If No, then you want to make the variables public or serialized private.
// You could use Find() ... Many ways ...
}

private void Update()
{
time -= Time.deltaTime;
if(time <= 0)
{
if(isPaused == false)
transform.Rotate(0,0, rotationalSpeed);
}
// Now here's where another script comes into play and
// I cannot guarantee translation because I can't see your scripts or objects they're attached.

if(score.GetComponent<Scores>().score >= boost)
{
rotationalSpeed += 0.05f;
arrows.Play();
boost += 500;
}
}

// This is where I stop. I have too many questions at this point to continue.
// The code above isn't going to be that accurate because
// I can't see the other scripts, and game objects they're attached to.

// OK EDIT --- I see what is going on you're referencing a script attached and
// accessing a constructor. This should still work.

But that’s pretty much all you have to know about translating the code. As far as your Animation goes, what is it attached to? This script? So the GetComponent<>() that I used, unless this script is attached to all of those items, this will not work.

That’s the best I can do with what you’ve given me to work with. I hope that you can see how this works and be able to fix the issue from here.

EDIT2 - I’ll continue the rest of the code here:

if(triggerFallBall.GetComponent<FallBall>().numberOfBalls1 == 9 && rotationSpeed > 0)
{
if(empty)
{
if(PlayerPrefs.GetInt("SoundBoolean") == 0)
{
triggerFallBall.GetGomponent<AudioSource>().Play();
}
empty = false;
}
rotationalSpeed -= 0.01f;
}

else if(triggerFallBall.GetComponent<FallBall>().numberOfBall2 == 9 && rotationalSpeed > 0)
{
if(empty)
{
if(PlayerPrefs.GetInt("SoundBoolean") == 0)
{
triggerFallBall.GetComponent<AudioSource>().Play();
}
empty = false;
}
rotationalSpeed -= 0.01f;
}

Now why the programmer made this such a pain in the ass is probably because of the old ways of coding. All he had to do was declare an AudioSource variable and it would have been a lot easier. I dislike his code format greatly! All around sloppy, if you ask me.

Final Edit - You GameManager Script

public bool paused
public static bool isPaused;

private void Start()
{
paused = false; isPaused = false;
}

private void Update()
{
if(pause == true)
{
Pause();
}
else{ UnPause(); }
}

public void PauseControl()
{
if(pause == false){ pause = true; ispaused = true; }
else{ pause = false; isPaused = false; }
}

public void Pause(){ Time.timeScale = 0; }

public void UnPause(){ Time.timeScale = 1; }
this is the score script....


private var empty : boolean = false;
var bestScore : int;
var score : int;
var basketCenter1 : Animation;
var basketCenter2 : GameObject;
var basketCenter3 : Animation;
var basketCenter4 : GameObject;
var triggerSemicircle1 : GameObject;
var triggerSemicircle2 : GameObject;
var emptyCircle : GameObject;
var landscapeFog : GameObject;
var wheel1 : GameObject;
var wheel2 : GameObject;
function Start(){
//load value bestScore
bestScore = PlayerPrefs.GetInt("BestScore");
}
function Update () {
//reset all points press D on keyboard.
if(Input.GetKeyDown(KeyCode.D)){
PlayerPrefs.DeleteAll();
}
//if the points scored better result,then store the value of points for best results
if(score > bestScore){
PlayerPrefs.SetInt("BestScore", score);
}
//We add up the points scored on two wheels
score = triggerSemicircle1.GetComponent(colorchange).score + triggerSemicircle2.GetComponent(colorchange).score;
//We deduce points on the screen
gameObject.GetComponent(GUIText).text = "" + score;
if(empty == true){
//If we score 1000 points
if(score == 1000){
//turn animation landscapeFog
landscapeFog.GetComponent.<Animation>().Play("LandscapeFogOpen");
}
//if the landscapeFog has become completely opaque
if(landscapeFog.GetComponent(SpriteRenderer).color.a == 1){
//assigned to the first wheel to a new position
wheel1.transform.position.x = -3.5;
//includes a second wheel
wheel2.SetActive(true);
//assign a value to a second rotation of the wheel
wheel2.transform.rotation = emptyCircle.transform.rotation;
//turn animation landscapeFog
landscapeFog.GetComponent.<Animation>().Play("LandscapeFogClosure");
//turn off the empty variable
empty = false;
}
}
//if the points earned is greater than 0 and less than 200
if(score > 0 && score < 200){
//to include an empty variable
empty = true;
}
//if the points earned more than 1000 and less 1300
if(score > 1000 && score < 1300){
//to include an empty variable
empty = true;
}
//if an empty variable and included points earned equal 4000
if(empty == true && score == 4000){
//turn animation basketCenter1
basketCenter1.Play();
//include basketCenter2
basketCenter2.SetActive(true);
//turn animation basketCenter3
basketCenter3.Play();
//include basketCenter4
basketCenter4.SetActive(true);
//to include an empty variable
empty = false;
}
}
and this is the ball Controller if you want this ...

var cam : Camera;
function Update () {
for (var touch : Touch in Input.touches) {
//if the finger is on the right side of the screen
if(touch.position.x > cam.pixelWidth / 2){
//add a ball to the right strength
gameObject.GetComponent(Rigidbody2D).AddForce(new Vector2(5,0));
}
//if the finger is on the left side of the screen
if(touch.position.x < cam.pixelWidth / 2){
//add a ball to the left strength
gameObject.GetComponent(Rigidbody2D).AddForce(new Vector2(-5,0));
}
}
//if you press the right arrow on your keyboard
if(Input.GetKey(KeyCode.RightArrow)){
//add a ball to the right strength
gameObject.GetComponent(Rigidbody2D).AddForce(new Vector2(5,0));
}
//if you press the left arrow on your keyboard
if(Input.GetKey(KeyCode.LeftArrow)){
//add a ball to the left strength
gameObject.GetComponent(Rigidbody2D).AddForce(new Vector2(-5,0));
}
}

and yes there are animations attach with this scripts … you mean i will not be able to use them ?

You can always use your animations. There’s not much difference on the syntax, but the difference between Unity 5 and 2018 is a gap. You can translate your variables the same way I shown above.

Your var cam : Camera

would look like this:

public Camera cam;

The only reason I know this is there’s no reference to the object “Camera” for GetComponent(), which insinuates there’s a camera placed within the inspector inside of that variable.

You display no desire to learn to code. Even your comments within the script are a syntax error. Do you not even know how to properly comment within a C# script?

I’ve helped you a lot. You can see the translation process and how it would work. If you cannot, then you are not ready for what you’re trying to do.

I suggest that you abandon your project and start a new one. One where you create your own graphics and code. I also want you to upgrade your Unity version to 2018.3 or higher. You need to learn the basics and build from there… I’ll help you, but no more on this.

ok brother , thx alot for your help :slight_smile: i know you did alot :)… may you give me any account to can contact with you ? like facebook or whatsapp or something like that

1 Like

I have just recently started a group for a Youtube tutorial series I’m working on. You can easily contact me here:
https://www.facebook.com/groups/935890136793648/
Should be a group called SkitzTutz by MrSkitz

sent a request

1 Like