- Home /
Pause not working correctly
So I have this script:
var firstPersonController:Transform;
var lantern:GameObject;
var paused : boolean = false;
function Start () {
inventoryText.guiText.text = "";
}
function Update () {
if (Input.GetKeyDown("p")&& paused == true){
inventoryText.guiText.text = "Lantern " + "[" + inventoryArray[0] + "]" + "\n"+ "Hit H to equip!";
//inventoryArray[0]++;
//inventoryArray[1] += 2;
paused = true;
if (Time.timeScale == 1.0)
Time.timeScale = 0.0;
}
if (paused == false){
inventoryText.guiText.text = "";
Time.timeScale = 1.0;
}
if (Input.GetKeyDown("h") && inventoryArray[0] > 0){
var go = Instantiate(lantern) as GameObject;
go.transform.parent = firstPersonController;
inventoryArray[0] -= 1;
}
if (Input.GetKeyDown("p")){
inventoryText.guiText.text = "Lantern " + "[" + inventoryArray[0] + "]" + "\n"+ "Hit H to equip!";
//inventoryArray[0]++;
//inventoryArray[1] += 2;
}
}
I want to be able to hit "p" and have the game pause as well as show the inventory, then when I hit "p" again I want it to not be paused and not show the inventory. However the script right now just blinks with the guitext shown when I hit "p" rather than pause the way I want it to.
Your code is a bit of a mess indentation wise, very hard to see what code belongs to what condition. Your logic seems to be equally messed up, you never set "paused = false" anywhere in the script you have given(only during your declaration at the top, which won't do anything again), so once it is true it will never become false again...
Sorry for the messy script, I'm extremely new to program$$anonymous$$g so I'm not very good with this. :(
Answer by Anxo · Nov 20, 2014 at 04:39 PM
Add a return.
if (Input.GetKeyDown("p")&& paused == true){
inventoryText.guiText.text = "Lantern " + "[" + inventoryArray[0] + "]" + "\n"+ "Hit H to equip!";
//inventoryArray[0]++;
//inventoryArray[1] += 2;
paused = true;
if (Time.timeScale == 1.0)
Time.timeScale = 0.0;
return; // <-- here
}
But better to handle things in outside methods, IMO so you can call it from other places like so. Forgive me, I write in C# not javascript but here we go
var isPaused : bool = false;
function Update (){
if(input.GetKeyDown(KeyCode.P)){
if(isPause){
UnpauseGame();
}
else{
PauseGame();
}
isPaused = !isPaused;
}
}
function PauseGame(){
InvantoryText.enabled = true;
Time.timeScale = 0;
}
function UnpauseGame(){
invantoryText.enabled = false;
Time.timeScale = 1;
}
A couple of typos there "isPause" and "InvantoryText", also you always set isPaused to !isPaused so the initial problem still remains(it needs to switch in relation to the "if(isPause){" statement conditions above it rather than after this).
function Update() {
if (input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P)) {
if (isPaused) {
isPaused = false;
UnpauseGame();
} else {
isPaused = true;
PauseGame();
}
}
}
isPaused = !isPaused; /// is the same thing as
if(isPaused){
isPaused = false;
}else {
isPaused = true;
}
// = )
And yes, sorry for the types, I should have added the WUCC tag to the end of my answer.
Ah, did not no that it worked that way(I always say true/false explicitly as I find it easier for me to read), cheers Anxo, +1 for you in return for today's lesson :)
Thank you very much! $$anonymous$$y apologies for the messed up script in the first place.
Your answer
Follow this Question
Related Questions
Real Simple Inventory and Vendor System Help 1 Answer
I need some help on inventory. 0 Answers
How do I reference a UI text in Unity 5? (JS) 2 Answers
Where can i begin to learn how to make an inventory. (Javascript). 1 Answer
Inventory Help. 2 Answers