Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by jw405 · Mar 17, 2016 at 02:18 AM · instantiateobjectsbooleanvoid

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);
         }
     }
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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")

Comment
Add comment · Show 5 · 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 jw405 · Mar 17, 2016 at 12:45 PM 0
Share

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?

avatar image Restemayer · Mar 17, 2016 at 07:04 PM 0
Share

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.

avatar image jw405 · Mar 18, 2016 at 03:59 PM 0
Share

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.

avatar image Restemayer · Mar 19, 2016 at 02:42 AM 0
Share

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.

avatar image Restemayer · Mar 19, 2016 at 02:44 AM 0
Share

Also... you're very welcome.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

giving instantiated GameObjects some properties? 1 Answer

Check a bool from a prefab? 0 Answers

How to change the Bool for only a single prefab GameObject creating with Instantiate? 2 Answers

How to find the (original) Prefab of a GameObject? 2 Answers

What kind of Void should I use? 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