- Home /
The question is answered, right answer was accepted
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
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);
}
}
}
}
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.
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);
}
}
}
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?
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.
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.
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.
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))