- Home /
Cannot Implicitly convert to bool problem?
I'm working on the main menu for my new IOS game and have run into a snag. I'm getting the error message:
Assets/SpaceCombatRPG-IOS Framework/Scripts/Non-Completed/MainMenu.cs(42,22): error CS0029: Cannot implicitly convert type MainMenu.Page' to
bool'
And yes, I do know what it means, but I cannot figure out the correct way to fix it. And I do not have any boolean variables which is the odd part. I'm guessing the problem seems to be with the way I am storing variables from my enumeration? Here's the code:
using UnityEngine;
using System.Collections;
public class MainMenu : MonoBehaviour {
private enum Page {
NewGame = 0,
Resume = 1,
LoadGame = 2,
Options = 3,
MoreGames = 4,
GameGuide = 5,
Addons = 6,
GameCenter = 7,
MainMenu = 8
}
private Page _currentPage;
public Texture2D gameNameTexture;
public Texture2D defaultButton;
public Texture2D addonsButton;
public Texture2D moreGamesButton;
public Texture2D gameGuideButton;
public Texture2D gameCenterButton;
void Start() {
_currentPage = Page.MainMenu;
}
void OnGUI() {
if(_currentPage = Page.MainMenu)
DisplayMainMenu();
else if(_currentPage = Page.NewGame)
DisplayNewGameMenu();
else if(_currentPage = Page.Resume)
DisplayResumeMenu();
else if(_currentPage = Page.LoadGame)
DisplayLoadGameMenu();
else if(_currentPage = Page.Options)
DisplayOptionsMenu();
else if(_currentPage = Page.MoreGames)
DisplayMoreGamesMenu();
else if(_currentPage = Page.GameGuide)
DispayGameGuideMenu();
else if(_currentPage = Page.Addons)
DisplayAddonsMenu();
else if(_currentPage = Page.GameCenter)
DisplayGameCenterMenu();
else
DisplayMainMenu();
}
public void DisplayMainMenu() {
GUI.BeginGroup(new Rect(0,60,500,300));
if(GUI.Button(new Rect((Screen.width / 2) - 100, 60, 200, 25), "New Game")) //Change the page to NewGame menu
_currentPage = Page.NewGame;
if(GUI.Button(new Rect((Screen.width / 2) - 100, 90, 200, 25), "Resume")) //Change the page to Resume menu
_currentPage = Page.Resume;
if(GUI.Button(new Rect((Screen.width / 2) - 100, 120, 200, 25), "Load Game")) //Change the page to LoadGame menu
_currentPage = Page.LoadGame;
if(GUI.Button(new Rect((Screen.width / 2) - 100, 150, 200, 25), "Options")) //Change the page to Options menu
_currentPage = Page.Options;
if(GUI.Button(new Rect((Screen.width / 2) - 250, 75, addonsButton.width, addonsButton.height), addonsButton))
_currentPage = Page.Addons;
if(GUI.Button(new Rect((Screen.width / 2) - 250, 135, moreGamesButton.width, moreGamesButton.height), moreGamesButton))
_currentPage = Page.MoreGames;
if(GUI.Button(new Rect((Screen.width / 2) + 250, 75, gameGuideButton.width, gameGuideButton.height), gameGuideButton))
_currentPage = Page.GameGuide;
if(GUI.Button(new Rect((Screen.width / 2) + 250, 135, gameCenterButton.width, gameCenterButton.height), gameCenterButton))
_currentPage = Page.GameGuide;
GUI.Label(new Rect((Screen.width / 2) - (gameNameTexture.width / 2), 10 - (gameNameTexture.height / 2), gameNameTexture.width, gameNameTexture.height), gameNameTexture);
GUI.EndGroup();
}
void Update() {
Debug.Log(_currentPage);
}
#region Functions not Templated
public void DisplayLoadGameMenu() {
}
public void DisplayNewGameMenu() {
}
public void DisplayResumeMenu() {
}
public void DisplayOptionsMenu() {
}
public void DisplayMoreGamesMenu() {
}
public void DispayGameGuideMenu() {
}
public void DisplayAddonsMenu() {
}
public void DisplayGameCenterMenu() {
}
#endregion
}
Answer by meat5000 · Sep 02, 2013 at 09:14 AM
use = to change a value. Use == to check a condition, like in an if statement.
The error means you are trying to put something thats not a bool in to a bool. It may happen if you check a variable for true or false thats not a bool. If you are trying to check if it exists use 'null'.
To be direct, change
if(_currentPage = Page.MainMenu)
DisplayMainMenu();
to
if(_currentPage == Page.MainMenu)
DisplayMainMenu();
and do the same for the rest of the if statements
Thanks, that was really quick. I've never really thought about the = and == statement differences so I'll have to remember that.
Your answer
Follow this Question
Related Questions
C# Enum for Animation Issue 1 Answer
Multiple Cars not working 1 Answer
C# Edit enum values in inspector 1 Answer
Distribute terrain in zones 3 Answers
not all lines of code running c# 0 Answers