Cannot implicitly convert type 'void' to 'boolean'
Hi everyone!
New to Unity, C# and programming! Checked for existing answers but haven't provided a solution.
I have a scene with GUI buttons that load a stage/map from another script. I want to instantiate certain prefabs for certain stages/maps.
So, for example if "Map 01" is loaded then create these objects.
I've given it a go but I get this error when I run the code! I'm guessing it's because my 'if' statement returns void instead of a boolean but I don't know how to change that. Any help would be HUGELY appreciated.
Error (InstantiateObjects Script below)
if (stage.Load (1)) {
InstantiateObject Script:
public class InstantiateObject : MonoBehaviour {
MapMaker stage;
public GameObject myObject;
public Vector3 spawnSpot = new Vector3(-4,0,5);
void Start() {
stage = GameObject.Find ("MapMaker").GetComponent<MapMaker> ();
}
void Update () {
if (stage.Load (1)) {
GameObject ObjectSpawn = (GameObject)Instantiate(myObject, new Vector3(-4,0,5), transform.rotation);
}
}
}
MapMaker Script:
void Start () {
public void Load (int stage_id) {
switch (stage_id) {
case 1:
MakeMap (new Map01 ());
break;
case 2:
MakeMap (new Map02 ());
break;
}
}
Game UI Script:
public class GameUI : MonoBehaviour {
MapMaker stage;
void Start () {
stage = GameObject.Find ("MapMaker").GetComponent<MapMaker> ();
}
void OnGUI () {
GUI.Box(new Rect(10,10,100,240), "Stages");
if(GUI.Button(new Rect(20,40,80,20), "Stage 1")) {
stage.Load (1);
}
if(GUI.Button(new Rect(20,70,80,20), "Stage 2")) {
stage.Load (2);
}
}
}
Answer by Restemayer · Mar 17, 2016 at 02:27 AM
Ok... here is the problem
This line
if (stage.Load (1))
the if statement is expecting a bool. if "true" execute... if "false", ignore...
The stage.Load function, however, doesn't return a bool as a value.
public void Load (int stage_id) {...
The if statement is calling it and getting an empty response as an answer.
for that to work, it would have to be
public bool Load (int stage_id) {..
and each switch would have to have a boolean return value ("return true" or "return false")
Restemayer, thanks for your reply!
1) Would the 'if' statement remain as it is then?
2) How would I edit the switch so that it returns a boolean value?
1) There are lots of ways of skinning that cat. It depends on what you are trying to do with it. What is the purpose of the if statement? Technically what I said wasn't technically accurate; an if statement takes a condition which must be true or false. A bool is the easiest because its defined as a simple true/false statement, but it can be other things. If you were putting an integer, float, or any other numeric value in there you could test to see if its equal to, greater than, or less than something else (x==5, x<=200, x>2, etc). You could test if a string meets certain conditions (weapon == "SHOTGUN"). You could even technically take a void, if you are testing for that (mustBeVoid == void, mustNotBeVoid != void). So, what are you trying to test with the stage.Load function? $$anonymous$$y assumption is that the function is just loading a scene before running whatever is in the if block. If that is what its doing, then I'd keep it as it is and just alter the return.
2) The way you return a value out of a function takes 2 things. First you have to declare in the function declaration that its going to be returning something. That is what "void" does in public void Load. Its telling the computer that its returning nothing, so don't look for a return value. Changing that to bool tells it to look for a return of a bool type. Second, is to actually give the return. in this case since it looks like all you want is to return true if a map loads, then its really easy. The command is just "return" followed by whatever value you are returning, so in this case "return true;". Every pathway needs a return value for it to be valid, so that line needs to be inserted into every block in your switch before the break. That should work.
As a side, I'd put in a default case in your switch that returns false. Where you have blocks for "case 1:", "case 2:", etc... at the end put in a block labelled "default:". What that does is it takes any value that fails to meet the conditions for any other case and returns false so that the if statement kicks it to your "else" condition ins$$anonymous$$d of running. If something wonky gets in there it'll prevent your program from crashing. Just general good practice.
Thanks Restemayer! As a beginner I often get sarcastic and unhelpful comments, I appreciate you taking the time out of your day to properly answer my question. I'm sure I can work it out from your answer and if not then i'll have to do further research on 'if' statements and returning boolean values.
I will change the stage.Load function to:
public bool Load (int stage_id) {..
AND then change the switch to:
case 1:
$$anonymous$$ake$$anonymous$$ap (new $$anonymous$$ap01 ());
break;
return true;
I'll add a 'default' too.
Thanks again.
You need it before the break. "Break;" tells the computer "this block is done; break out of it and return to the next higher level of code". Basically it's a hierarchy. When the computer sees a switch, it knows there are cases inside that switch. It takes whatever value it is fed and looks at the cases until it finds one that matches. Then it knows there is code inside that case, which it runs. When it breaks, that block is concluded and moves out of it, so putting the return after the break command means it will never run the return command.
Your answer
![](https://koobas.hobune.stream/wayback/20220612072105im_/https://answers.unity.com/themes/thub/images/avi.jpg)