Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by MoonJaspers · Jan 09, 2017 at 01:43 PM · if-statementselseelse if

Else and If activated at the same time?

So before the bug, i was working on my project until when i came back to it a script was missing, so i placed it back including the public things you drag gameobjects and such into. I'm not sure if this is relevant but I feel the need to say this because it may be the main culprit.

Anyways, I was working on a project and one of the things I was working on was where a character would enter a certain village (with a collider and a OnTriggerEnter, with bools, holding data as well), and one of the buttons would appear and if you clicked on it you would go onto a village menu. I was learning while making this, and was amazed to find my concept to be perfectly working last night. I called it a day and closed unity (I also saved it, so that's that)

But then this morning when I opened the project up again, I played it again to see my amazing baby-step accomplishment only to find that the button would not work as intended when clicked. The button would appear when my character is on top said village, but the button's 'Interactable' state would always stay off (checked in inspector's tab during play mode). I looked through the script and placed debug.logs to see if the collision and such had been working:

     void Start () {
         //objectholdingexecuteLocalMenu.GetComponent<executeLocalMenu>().enterButton.interactable = false;
 
         colorToFadeTo = new Color(0.5f, 0.5f, 0.5f, 0.5f);
         enterButton.CrossFadeColor(colorToFadeTo, fadeTime, true, true);
         objectholdingexecuteLocalMenu.GetComponent<executeLocalMenu>().enterButton.interactable = false;
     }
     
     
     void Update () {
         if (inBahpTown == true)
         {
             //Gets variable "recruits" and assigns variable to variable "recruitCard"
             //recruitCard = objectholdingTown_Data.GetComponent<bahpTown_Data>().recruits;
 
             colorToFadeTo = new Color(1f, 1f, 1f, 1f);
             enterButton.CrossFadeColor(colorToFadeTo, fadeTime, true, true);
             titleCard = ("Bahp Town");
             infoCard = ("You currently have " + relationCard + " relations with this " + rankCard + ". It is also " + wealthCard);
             relationCard = 0;
             rankCard = "Town";
             wealthCard = "Poor.";
             objectholdingexecuteLocalMenu.GetComponent<executeLocalMenu>().enterButton.interactable = true;
             Debug.Log("You are touching the town.");
 
 
         }
         else
         {
             colorToFadeTo = new Color(0.5f, 0.5f, 0.5f, 0.5f);
             enterButton.CrossFadeColor(colorToFadeTo, fadeTime, true, true);
             inBahpTown = false;
             titleCard = "Null";
             infoCard = "";
             //objectholdingexecuteLocalMenu.GetComponent<executeLocalMenu>().enterButton.interactable = false;
             Debug.Log("You're not touching anything.");
         }
     }
 
     void OnTriggerEnter2D(Collider2D collision)
     {
         //objectholdingexecuteLocalMenu.GetComponent<executeLocalMenu>().enterButton.interactable = true;
         if (collision.gameObject.tag == "bahp_town")
         {
             inBahpTown = true;
         }
             
     }
      void OnTriggerExit2D(Collider2D collision)
     {
         colorToFadeTo = new Color(0.5f, 0.5f, 0.5f, 0.5f);
         enterButton.CrossFadeColor(colorToFadeTo, fadeTime, true, true);
         inBahpTown = false;
         Debug.Log("Leaving location.");
 
         objectholdingexecuteLocalMenu.GetComponent<executeLocalMenu>().enterButton.interactable = false;
     }
 }
 

I tried it again and I saw the log would continuously say "You're not touching anything" as fully expected. Then when the character collided with the village's collision box something bamboozled and confused me severely. Both debug logs would show up in this order:

 You're not touching anything
 UnityEngine.Debug:Log(Object)
 You're touching the town
 UnityEngine.Debug:Log(Object)
 You're not touching anything
 UnityEngine.Debug:Log(Object)
 You're touching the town
 UnityEngine.Debug:Log(Object)

Bug in action: https://www.dropbox.com/s/jbmgj1500b5bgv7/clip0025.avi?dl=0

Both debug.logs from both if and else were playing at the same time! How is this possible? Is it actually playing both ifs and else? I need help, I really don't want to remake this game all over again! I tried "else if (inBahpTown == false)" instead of "else". I tried moving some things around and eventually I managed to fix the button problem. But that would leave the rest of my data left in the else statement which would be used later on in another script to be immediately changed to "Null" and 0, float and string wise.

Can someone point out the flaw in my script? If you need anymore information you could use to help me I'd be glad to tell you.

Thanks in advance.

Comment
Add comment
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

1 Reply

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

Answer by ScaniX · Jan 09, 2017 at 01:48 PM

IF and ELSE are never active "at the same time". The only way for the debug log to happen is if they get activated alternatingly or - and this is what I think is happening here - there is more than one instance of the script.

There is only one place setting the bool to false (line 32 can be ignored as the variable is already false in that case). That place would print out "Leaving location".

So I guess you have two instances of the script and only one of them is connected to a collider that actually sets the bool inBahpTown to true, while the one of the other instance stays false.

To check this, you can print out the instance id as well.

 Debug.Log(gameObject.GetInstanceID());

PS: The video kinda supports my assumption. It seems there is one instance that is moved with your player and changes state when colliding and one instance that stays in the "false" state.

Comment
Add comment · Show 1 · 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 MoonJaspers · Jan 09, 2017 at 11:12 PM 0
Share

So you mean to say there might be another copy of this script? That makes sense... I'll check the GetInstanceID and I'll come back with a result.

EDIT: Turns out there was another instance of the same script! The whole thing works now, thanks!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Cursor Lock Code w/ if, else if, and else Statement Errors 1 Answer

IF Else Statement being ignored ,the else if condtion is being ignored and playing first IF condition for all game objects. 0 Answers

If statement being ignored 2 Answers

code so if 1 enemy isnt killed every 4 seconds player dies? 1 Answer

Need Help With Simple Java Script Regarding GUI 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