- Home /
if statements is not working properly
This is JS
I attached script B to generators (When you press "E", value of "CountGenerator" in Script A will added 1 or increase)
//Script B
var message : boolean = false;
var GeneratorGUI : GameObject;
var Generator1 : GameObject;
var useGui : boolean = true;
function OnTriggerStay(other : Collider)
{
if(Input.GetButton("e"))
{
GeneratorGUI.GetComponent(GeneratorCountNumbers).addCount(1);
disableGui();
}
}
function disableGui ()
{
Generator1.GetComponent(ActiveGenerator).useGui = false;
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
message = true;
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
message = false;
}
}
function OnGUI()
{
if(message)
{
if(useGui)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Press E to turn on the generator");
}
}
}
I attached Script A to empty gameobject called "GeneratorGUI"
//Script A
static var CountGenerator : int = 0;
var Onemore : GUIText;
var Activated : GUIText;
var Gate : GameObject;
var GateOpenSound : AudioSource;
function addCount(value : int)
{
CountGenerator += value;
if(CountGenerator == 1)
{
Onemore.enabled = true;
yield WaitForSeconds(3);
Onemore.enabled = false;
if(CountGenerator == 2)
{
Activated.enabled = true;
yield WaitForSeconds(3);
Activated.enabled = false;
Gate.animation.Play("OpenGate");
GateOpenSound.Play();
}
}
}
I play the game. I activate one generator by clicking "E" and "OneMore" is enabled because I add 1 value to CountGenerator. That's good. But when I activate one more generator by clicking "E", "Activated" is not enabled and the rest is not working. I said if int = 2, "Activated" should be enabled but it's not.
Btw, I don't get any errors
Final Results (SOLVED)
Script A
//Script A
static var CountGenerator : int = 0;
var Onemore : GUIText;
var Activated : GUIText;
var Gate : GameObject;
var GateOpenSound : AudioSource;
function addCount(value : int)
{
CountGenerator += value;
print (CountGenerator);
if(CountGenerator == 1)
{
Onemore.enabled = true;
yield WaitForSeconds(3);
Onemore.enabled = false;
}
else if(CountGenerator == 2)
{
Activated.enabled = true;
yield WaitForSeconds(3);
Activated.enabled = false;
Gate.animation.Play("OpenGate");
GateOpenSound.Play();
}
}
Script B
//Script B
var GeneratorGUI : GameObject;
var Generator1 : GameObject;
var useGui : boolean = true;
var isInside : boolean = false;
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
message = true;
isInside = true;
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
message = false;
isInside = false;
}
}
function Update()
{
if (isInside && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
GeneratorGUI.GetComponent(GeneratorCountNumbers).addCount(1);
disableGui();
}
}
function disableGui ()
{
Generator1.GetComponent(ActiveGenerator).useGui = false;
}
function OnGUI()
{
if(isInside)
{
if(useGui)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Press E to turn on the generator");
}
}
}
Thanks to vexe
Answer by vexe · Oct 05, 2013 at 07:36 AM
There's a very obvious hole in your logic, you're saying:
if (fruit is an apple)
{
if (fruit is an orange) {
}
}
Well, if fruit
was an apple, we would get inside the first if
, right? does it make any sense now to check if fruit
is an orange
? of course not! we already know it's an apple
by now. OK, now what if fruit
was an orange
, can we get inside the 2nd if? no, cause we have to enter the first if
first, and since we're an orange
, we can't :) - Hope that made sense.
Change your logic:
if(CountGenerator == 1)
{
Onemore.enabled = true;
yield WaitForSeconds(3);
Onemore.enabled = false;
}
else if(CountGenerator == 2)
{
Activated.enabled = true;
yield WaitForSeconds(3);
Activated.enabled = false;
Gate.animation.Play("OpenGate");
GateOpenSound.Play();
}
Why if-else
and not just two ifs? well, you can't be both an orange
and an apple
at the same time, you're either an orange
, or an apple
- Which means there's only one block of code should be executed, doing it in an if-else ensures that only one check is performed. If you did it like this:
if (fruit is an orange)
//
if (fruit is an apple)
//
This is redundant, because if it was an orange
, we don't need to check if it was an apple
, we already know it's not :)
And btw you might run into trouble using Input.GetXXX
inside OnTriggerStay
- it's hazardous, I recently had problems with it in that whatever I put inside the if (Input.GetXXX)
gets executed more than once, see this.
You are probably right. How in the hell apple will be able to change to orange. It makes any sense now but problem still not solved
//Script A
static var CountGenerator : int = 0;
var Onemore : GUIText;
var Activated : GUIText;
var Gate : GameObject;
var GateOpenSound : AudioSource;
function addCount(value : int)
{
CountGenerator += value;
if(CountGenerator == 1)
{
Onemore.enabled = true;
yield WaitForSeconds(3);
Onemore.enabled = false;
}
else if(CountGenerator == 2)
{
Activated.enabled = true;
yield WaitForSeconds(3);
Activated.enabled = false;
Gate.animation.Play("OpenGate");
GateOpenSound.Play();
}
}
When you activated just ONE generator, it still access (CountGenerator == 2) although I just add 1 value only to CountGenerators in Script B.
Heh, of course the print doesn't solve it, it just prints stuff ^^ - I just wanted you to see that the value of your generator is being incremented more than once when you're doing the if(Input.GetButton("e"))
Well, actually for starters you shouldn't even use GetButton
, you should try to use GetButtonDown("e")
or Get$$anonymous$$eyDown($$anonymous$$eyCode.E)
because GetButton
will return true while
the button is pressed, even if you press it for a little while it could return true more than once. GetButtonDown
will return true only
at the first frame the button got pressed in, after that it's false.
I also think that you will have problems even when you use this, if so, please check out the link I gave you, for everything I will say to you after that is well written about/explained in that link.
Here:
function OnTriggerEnter(other : Collider)
{
if (other.gameObject.tag == INSERT_YOUR_TAG_HERE)
isInside = true;
}
What you mean they don't work? - Check to see that your player has the right tag. - Also, you don't have brackets around your if
body, in your Update
, do:
if (isInside && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
GeneratorGUI.GetComponent(GeneratorCountNumbers).addCount(1);
disableGui();
}
And btw, you don't need message
any more, to me it seems it's doing nothing more than what isInside
is doing, just use isInside
ins$$anonymous$$d.