Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to put multiple actions in an if statement inside of a function?

Discussion in 'Getting Started' started by Sylvir, Apr 27, 2015.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    I thought that i had a handle on how this works, but I am finding out tonight that I actually do not.

    here is the code snipit that I am working on at the moment.

    Code (CSharp):
    1.  
    2.     public void OnClick()
    3.     {
    4.         if (IdleUpgradeWindow.SetActive (true))
    5.         {
    6.             IdleUpgradeWindow.SetActive (false);
    7.             PowerUpgradeWINDOW.SetActive (true);
    8.         }
    9.         else (IdleUpgradeWindow.SetActive (false))
    10.         {
    11.                     IdleUpgradeWindow.SetActive (true);
    12.                     PowerUpgradeWINDOW.SetActive (false);
    13.         }
    14. }
    15.    
    I have 2 public GameObjects that are the IdleUpgradeWindow and PowerUpgradeWINDOW. basically i have 2 windows that are the same size and in the same location, what I am attempting to do is to get a button click to check witch one is active, and then swap that one to inactive and swap the inactive one to active.

    seems im getting a parsing error, but im not sure where, it is saying its in that function though
     
  2. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    here is the whole code if that helps.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class OpenIdleUpgradesWindow : MonoBehaviour {
    5.  
    6.     public GameObject IdleUpgradeWindow;
    7.     public GameObject PowerUpgradeWINDOW;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.    
    17.     }
    18.  
    19.     public void OnClick()
    20.     {
    21.         if (IdleUpgradeWindow.SetActive (true))
    22.         {
    23.             IdleUpgradeWindow.SetActive (false);
    24.             PowerUpgradeWINDOW.SetActive (true);
    25.         }
    26.             else (IdleUpgradeWindow.SetActive (false))
    27.         {
    28.                     IdleUpgradeWindow.SetActive (true);
    29.                     PowerUpgradeWINDOW.SetActive (false);
    30.         }
    31.     }
    32. }  
    33.  
     
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Read your error messages, man. If you did, I'm sure you'd notice your error is happening on line 25/26, not inside your if statement, but rather with your else portion. You can't check for a condition with just 'else'. You need to do an else if, or just else and then run the code. Look up how to do a proper if/else in C#.
     
  4. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    the reason i said it was happening inside the function is beacuse that is what the error messages said, sorry for bothering you with this. I am in the process of learning, and i did not mean to interupt your day. Wasn't looking to get attacked just had a simple question, and i said that is what the error messages said on my console.
     
  5. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    also else if doenst work it says i cant change a void to a bool. that is the other reason i asked.. i did try that in the first place
     
  6. Gametyme

    Gametyme

    Joined:
    May 7, 2014
    Posts:
    618
    Try changing void to bool.
     
  7. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    thank you gametyme
     
  8. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    just public bool OnClick(){} instead of the public void OnClick(){}?
     
  9. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    Code (CSharp):
    1. public bool OnClick()
    2.     {
    3.         if (IdleUpgradeWindow.SetActive (true)) {
    4.             IdleUpgradeWindow.SetActive (false);
    5.             PowerUpgradeWINDOW.SetActive (true);
    6.         }
    7.                 else if (IdleUpgradeWindow.SetActive (false))
    8.             {
    9.                     IdleUpgradeWindow.SetActive (true);
    10.                     PowerUpgradeWINDOW.SetActive (false);
    11.             }
    12.        
    13.     }
    now its saying cant convert void to bool again
     
  10. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Here is your problem, well at least I think it is
    You are trying to check if(IdleUpgradeWindow.SetActive(true))
    SetActive() is a void function
    Void functions are functions that have no return, and when you use a function as an condition for an if statement, you are actually looking at its return value
    Can you see why it's actually impossible to use void functions to check things?
    SetActive() has the purpose of changing the active state of an object, *not* checking it
    For checking it I believe you would want activeInHierarchy
    activeInHierarchy is a variable every game object has, true if it is active, false otherwise
    This is what you want to check to see if it's active
    Try
    Code (CSharp):
    1. public void OnClick()
    2.     {
    3.         if (IdleUpgradeWindow.activeInHierarchy) {
    4.             IdleUpgradeWindow.SetActive (false);
    5.             PowerUpgradeWINDOW.SetActive (true);
    6.         }
    7.                 else if (!IdleUpgradeWindow.activeInHierarchy)
    8.             {
    9.                     IdleUpgradeWindow.SetActive (true);
    10.                     PowerUpgradeWINDOW.SetActive (false);
    11.             }
    12.      
    13.     }
    Also just incase you are unaware putting ! in front of a condition reverses it, or makes it a not statement

    Hope that helped, let me know if it works
     
    Last edited: Apr 27, 2015
  11. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I'm not trying to attack you, @Sylvir. You just... post a lot of threads, asking a lot of questions that could easily be found out by Googling, or taking some time to learn proper coding techniques. I'm all for being active in the community and getting help when you need it, but at the rate you post, it doesn't seem like you spend a lot of time trying to figure things out yourself before asking for help. Debugging issues and solving them yourself is one of the most effective ways of learning how code works, and wracking your brain against something for a bit can only help you grow as a developer.

    For example, you posted 5 times in this thread in the last half hour. During that time, one of your issues was solved, and another arose. Instead of spending some time figuring out that new problem, you just posted the error and asked for help solving it. Spend a bit of time trying to figure out what the error is telling you, and learn from it.

    That's just my advice. I won't criticize you again, but if you don't take the time to learn, don't be surprised to see the number of people willing to assist you start to dwindle.
     
    ztanim and blizzy like this.
  12. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    im sorry that you feel that way, i post alot of questions yes, I also spend hours looking up stuff on my own and not even knowing where to start looking. so if i ask for some help in the direction to search or why something isnt working thats what it thought the getting started forums were for, you know.. people who are new and looking for some help and ideas.
    i spent my time last night for hours trying to work this thing out before i was asking last night then this morning
    thanks QSFW ill do some research on that function, and yes that makes sence as to why it wouldn't work. thank you!
     
  13. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Whereas I am happy to help you, I do have to agree with @Schneider21
    Trust us, we've gone through the learning process, we were all n00bs one day
    And where as searching on forums is good, you should always give it a go your self. Really look at those errors, think about what they really mean and give it your best effort to solve it yourself, programming is all about problem solving, and practice makes perfect
     
    Schneider21 likes this.
  14. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Don't apologise @Sylvir
    We aren't having a go at you, and you are obviously new to this, we are just trying to help you in the way that will best help you, which is helping you to help yourself :)
     
  15. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    appears that that idea does not all return a value, i will see if i can find out why in the documentation, thanks for the help guys. i hope this is the right direction to work out how to accomplish what i am trying to do!
     
  16. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Okay @Sylvir, here's something you can do
    Do yourself a favour and do some research on functions and the different types, it will save you a hella lot of trouble
    What isn't returning a value, the void function? (it shouldn't, hence void) or the activeInHeirarchy?
     
  17. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Again, I'm not trying to insult, demean, or badger you, Sylvir. Look through my post history and you'll see I'm always happy to help people and provide encouragement, positive suggestions or code assistance. Yes, the Getting Started board is for new members to get comfortable with Unity, and I wouldn't even be browsing it and responding to your thread if I wasn't willing to help. As QFSW said, not asking for help will make you a better programmer. If you get stuck on something for a while, sure, by all means ask. But there's no way you really tried solving the void-to-bool issue in the 10 minutes between posts.

    @Sylvir, get a book on C# and learn basic programming fundamentals. It would take you like a month tops to get through, I bet. That'll solve like 80% of the issues I've seen you posting about, and the rest, that are actually about C# in the context of the Unity environment, we'll be happy to help with here. "Void" means there is no return value, though. That's just a C# thing, not specific to Unity.

    This thread is moving so fast, the last 4 posts have been as I was typing this, lol.
     
  18. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    True, it's funny how we are all active here at the same time, becoming a chat room... :)
    Anyway going with what @Schneider21 said, I totally agree
    I've never used books for learning, but I heavily use documentations and I've had several years of programming experience (C++, C#, Python, Java etc.) before coming into Unity, so I can highly recommend at least learning the ropes with C# and programming in general, and being able to program some basic things without help, after that you are ready for the fun stuff like Unity. :)
     
    Schneider21 likes this.
  19. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Sorry just made a change to my code, make sure your OnClick() function is void again since it has no return, forgot to change it when copying your code
     
  20. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    Thanks guys, and @Schneider21 specificly, sorry about my comments earlier about feeling like i was being attacked i realized later this morning that i miss read what you said badly. So i apologize for that, didnt mean to make any harsh feelings or anything. Thank you again for the tips. I am also going to be signing up for this class when i have the money as well as going through all the unity teaching and C# videos from the start.

    https://www.udemy.com/unitycourse/?...309-0000650f3c51_51936118&utm_medium=udemyads

    I will be more proactive and try to post more when i am really stumped after along time of searching/reading etc.

    hope you both have great days, and again i am sorry for eariler as miss reading things can lead to many mistakes in communication AND programing >.< :p
     
    Schneider21 likes this.
  21. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Thank you @Sylvir, you have a great day too
    I'm glad that you are going to learn some general program
    Btw remember for non unity related programming issues try stack overflow, chances are if you search someone has already asked it, and if not then it's full of talented programmers and you get the answer fast without any of the chit chat
    OK as a closing note remember, error messages are your friend, that's why they are there
     
    Schneider21 likes this.
  22. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    hmm, i will look up stack overflow thanks! have not heard of that one yet. thanks for the tips!
     
  23. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Whenever you actually Google an error message, Stack Overflow is almost always one of the first sites to come up in the results. Not surprised you never saw it. ;)

    Just a quick warning: If you thought we were being hard on you here, you might not want to ask any questions on SO.

    @Ryiah had posted a link to a free online C# book before. Maybe he/she/it could do so again for your benefit. Should hold you over until you can afford that class, I imagine.
     
    ztanim likes this.