Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Alanj2007 · Oct 05, 2013 at 06:32 AM · guiif-statementsenableinteger

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alanj2007 · Oct 05, 2013 at 08:48 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 17 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alanj2007 · Oct 05, 2013 at 07:44 AM 0
Share

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.

avatar image vexe · Oct 05, 2013 at 08:00 AM 1
Share

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.

avatar image vexe · Oct 05, 2013 at 08:07 AM 1
Share

it's Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) :)

avatar image vexe · Oct 05, 2013 at 08:30 AM 1
Share

Here:

 function OnTriggerEnter(other : Collider)
 {
     if (other.gameObject.tag == INSERT_YOUR_TAG_HERE)
     isInside = true;
 }
avatar image vexe · Oct 05, 2013 at 08:42 AM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Activating GUI 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

What is the best way to concatenate string to create an interaction logging console? 4 Answers

How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges