Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Nov 19, 2014 at 01:32 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by BlackPhoenixDE · Nov 17, 2014 at 08:52 PM · convertboolifvoid

Why does Unity mean with :Cannot implicitly convert type `void' to `bool' ?

At the very beginning: I'm a beginner. I have a problem with my little Game. I want to create a hardcore mode where the Walls are deactivated. This is my script:

 using UnityEngine;
 using System.Collections;
 
 public class Hardcore : MonoBehaviour
 {
     public GameObject Walls;
     void Update ()
     {
         if (Input.GetKeyUp(KeyCode.H))
         {
             if (Walls.SetActive(true))
                 {
                 Walls.SetActive(false);
                 }
 
             else if(Walls.SetActive(false))
                 {
                 Walls.SetActive(true);
                 }
         }
     }
 }

Unity says: Assets/Scripts/Hardcore.cs(16,30): error CS0029: Cannot implicitly convert type void' to bool' What does that mean? What can I do?

Thanks and Sorry for mistakes in language. My english is not the best.

Jakob

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

3 Replies

  • Sort: 
avatar image
4
Best Answer

Answer by Shbli · Nov 18, 2014 at 10:25 AM

Hi

Your basic problem is that if statements are used with conditions, a type of code that can say true or false (Gets that value, not sets it)

SetActive does actually set an object as active, but with it you can't check (Get) to know if the object is active or not

You have to call this "GameObject.activeInHierarchy"

So to fix your code

 using UnityEngine;
 using System.Collections;

 public class Hardcore : MonoBehaviour
 {
     public GameObject Walls;
     void Update ()
     {
         if (Input.GetKeyUp(KeyCode.H))
         {
             if (Walls.activeInHierarchy == true)
             {
                 Walls.SetActive(false);
             }
             else if(activeInHierarchy == false)
             {
                 Walls.SetActive(true);
             }
         }
     }
 }


But, hey your code isn't optimized, if you already did if check, then in the else section, it means that the object isn't really active, so properly there's no need to check "If == false" you can just keep the else part with additional if! Here's a more optimized code let's say

 using UnityEngine;
 using System.Collections;

 public class Hardcore : MonoBehaviour
 {
     public GameObject Walls;
     void Update ()
     {
         if (Input.GetKeyUp(KeyCode.H))
         {
             if (Walls.activeInHierarchy == true)
             {
                 Walls.SetActive(false);
             }
             else
             {
                 Walls.SetActive(true);
             }
         }
     }
 }
Comment
Add comment · Show 2 · 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 BlackPhoenixDE · Nov 18, 2014 at 02:41 PM 0
Share

Thank you. Good explanation!

avatar image wavenquack24 BlackPhoenixDE · Aug 05, 2020 at 05:50 AM 0
Share

Thank you so much! I spent an hour to find the solution and still failed. But then i decided to come here to search for an answer and there it is! I appreciate it.

avatar image
2

Answer by robertbu · Nov 17, 2014 at 08:59 PM

SetActive() does not return a boolean value. You can check the state with activeSelf. As for your code, it can be simplified to:

 using UnityEngine;
 using System.Collections;
 
 public class Hardcore : MonoBehaviour
 {
     public GameObject Walls;
     void Update ()
     {
         if (Input.GetKeyUp(KeyCode.H))
         {
             Walls.SetActive (!Walls.activeSelf);
         }
     }
 }
Comment
Add comment · Show 7 · 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 Owen-Reynolds · Nov 18, 2014 at 06:46 AM 0
Share

Sure, it would work. But suggesting nested functions and a unary bang to someone who is still learning to read a simple function prototype? Can't you just plan how to kill Batman?

avatar image Kiwasi · Nov 18, 2014 at 09:02 AM 2
Share

Not sure what the problem is. This solution is far better then the mass of ifs running around in the OP. Far better to $$anonymous$$ch correct code then pussyfoot around it because they might not understand the syntax.

I still remember the aha moment the first time I saw paused = !paused.

avatar image BlackPhoenixDE · Nov 18, 2014 at 02:40 PM 0
Share

Thanks! It works!

avatar image Owen-Reynolds · Nov 19, 2014 at 01:07 AM 0
Share

A little meta, but: it's common here to have a Q from someone who is clearly a very, very novice programmer, and to have people throw way too many concepts at once.

It's easy to forget just how long it takes to learn that bools are also variables and can be operated on; or that functions can take anything that evaluates to the proper type.

I feel like too many novices are getting code written for themselves here, but not at a level they can understand. And it hurts them in the long run. It's like someone who's been run-through all the dungeons. They have cool gear, but never learned to manage aggro or not to stand in green stuff.

avatar image Kiwasi · Nov 19, 2014 at 01:49 AM 0
Share

Agree to disagree then. I've personally copied and pasted code from the internet that works without understanding it, then gone back and learnt what was happening later when my interest and ability allowed it. I don't believe its hurt my ability to code.

Either way multiple answers are up and available for future viewing.

Show more comments
avatar image
1

Answer by Jeff-Kesselman · Nov 17, 2014 at 08:53 PM

Walls.SetActive is a function that returns a void.

You are trying to use it as if it returns a boolean value (true or false) in this line.

  else if(Walls.SetActive(false))
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 meat5000 ♦ · Nov 17, 2014 at 08:56 PM 0
Share

http://docs.unity3d.com/ScriptReference/GameObject-activeSelf.html

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

How to make a file run if a void has undefined coordinates? 1 Answer

I'm having problems with bools resetting within a function 0 Answers

How actions in if() are carried out depending on booleans? 1 Answer

Convert bool to char in C# 3 Answers

How to make an if command check a bool (or any other variable) only if it has changed 1 Answer


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