can you use if statements inside a void function?

i thought that this would work just fine, but its saying i cant implicitly convert type void to bool.

here is my code. i can get it working with 1 script to make the game object become active, and then another script and button to make it be not active, but i thought i could easily do this so the same button would activate or deactivate the gameobject. here is the code i have..

using UnityEngine;
using System.Collections;

public class OpenPowerUpgrades : MonoBehaviour {


    public GameObject PowerUpgradeScreen;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    public void OnClick()
    {
        if (PowerUpgradeScreen.SetActive(false));
            PowerUpgradeScreen.SetActive (true);
        if (PowerUpgradeScreen.SetActive(false));
        PowerUpgradeScreen.SetActive (true);


    }           



}

the void OnClick is what it is having the issues with. any help would be awesome, thanks everyone for taking the time to read this!

An if-statement tries to determine if the expression in parenthesis is true or false. In this case the expression is trying to pass the return value from SetActive, but SetActive does not return a value. If you want to read the active state, you need to use activeSelf instead.

public void OnClick()
{
   if (PowerUpgradeScreen.activeSelf == false)
      PowerUpgradeScreen.SetActive(true);
   else
      PowerUpgradeScreen.SetActive(false);
}
2 Likes

oh, okay, i think that i understand now. thank you for responding. so basically i have to tell it to check first using activeself and then i can check if its on or off, and then tell it what to do? thanks.

The code above is checking if the object is active (true) or not (false). If it is not active, it calls SetActive(true) and if it is active, it calls SetActive(false).

1 Like

okay, got it now. thanks!

There is another way to do it as well if you're curious. Since activeSelf returns a boolean and SetActive takes a boolean, you can simply pass the opposite value of activeSelf into SetActive. You can do this with an exclamation mark.

public void OnClick()
{
   PowerUpgradeScreen.SetActive(!PowerUpgradeScreen.activeSelf);
}
1 Like

oh cool, thanks for showing me that as well, i am trying to learn C# as well as unity at the same time. I have not been able to start my actual online class yet for c# (money hold up) but i should be able to start it in the next week or so. Thanks again for all the help youve been. I have learned alot in great deal to you and others on this site helping me out with my questions.

I always thought with if statements you had to put {} around the action if the statement is true.

I can't see that in this code. Does this mean you always have to use the {} brackets?

[quote=“Simpso_1”, post:8, topic: 577493]
I always thought with if statements you had to put {} around the action if the statement is true.

I can’t see that in this code. Does this mean you always have to use the {} brackets?
[/quote]

If statements will execute the next statement block. So that means the next line starts with a brace it will execute until the next closing brace. Or if there is no brace it will just execute the next line.

Use this with caution. Its easy to mess up as a new coder and forget this rule. But for very short one off events it can save typing and make the code easier to read. This is how I most often use it.

if (scriptIsOff) return;
3 Likes