Why can't monodevelop find this public method in my component?
I'm scripting an 'offlineGUI' to manage some GUI in a little 2D game, but I'm having trouble accessing a certain public method in my game player component:
GetComponentInChildren<offlinePlayer> ().getBool("goAhead")
When I try to build, the above line gives the following error:
Type 'offlinePlayer' does not contain definition for 'getBool' and no extension method 'getBool' of type 'offlinePlayer' could be found. Are you missing an assembly reference?
The 'getBool' method in my 'offlinePlayer' class is public. I can access other public methods from that class in my 'offlineGUI' by using the GetComponentInChildren() method without issue. Thanks for any answers!
Edit: Here's the offlinePlayer class: using UnityEngine; using System.Collections; using UnityEditor.SceneManagement;
public class offlinePlayer : MonoBehaviour {
//switches
private bool running; //is the game running?
private bool goAhead = true; //used to bring the game from not running to running
private bool timeUp; //is time less than 0?
private bool newGame; //is the game just beginning?
private bool gameOver; //turned on once a team gets the max points
//time variables
public float roundTime; //time per round
public float timer;
//team scores
private int redScore;
private int blueScore;
//point button measurements
public float buttonWidthRatio = 0.3f;
public float buttonHeightRatio = 0.2f;
public float buttonXRatio = 0.1f;
public float buttonYRatio = 0.1f;
//game over button measurements
public float gameOverButtonWidthRatio = 0.5f;
public float gameOverButtonHeightRatio = 0.5f;
public float gameOverButtonXRatio = 0.1f;
public float gameOverButtonYRatio = 0.1f;
public void signalStart(){
if (newGame) {
redScore = 0;
blueScore = 0;
newGame = false;
}
timer = roundTime;
if (!running && goAhead) {
GetComponentInChildren<gametext> ().startSignal ();
running = true;
timeUp = false;
goAhead = false;
}
}
public bool getBool (string name){
switch (name){
case "goAhead":
return goAhead;
case "timeUp":
return timeUp;
case "newGame":
return newGame;
case "gameOver":
return gameOver;
}
}
public bool checkStatus () {
return running;
}
public void toggleStatus () {
if (running)
running = false;
else
running = true;
}
void Update() {
if (running) {
timer -= Time.deltaTime;
if (timer < 0.0) {
running = false;
timeUp = true;
GetComponentInChildren<countdown> ().timeUp ();
}
}
}
void addPoints(int team, int points){
if (team == 1) {
if (points == 1)
redScore++;
else
redScore += 2;
} else {
if (points == 1)
blueScore++;
else
blueScore += 2;
}
//logic for if one team wins
if(redScore>5||blueScore>5){
gameOver = true;
}
}
Why are you you creating a new method to get bools? why not just use properties or enums?
Answer by Jessespike · Jun 28, 2016 at 10:00 PM
The method cannot be found, because scripts with errors will not compile.
Specifically the issue is that getBool expects to return a bool, but there is a case where nothing is returned.
error CS0161: `offlinePlayer.getBool(string)': not all code paths return a value
public bool getBool (string name){
switch (name){
case "goAhead":
return goAhead;
case "timeUp":
return timeUp;
case "newGame":
return newGame;
case "gameOver":
return gameOver;
}
// fallback if no matching case exists
Debug.Log("Unable to get bool: " + name);
return false;
}
Your answer
Follow this Question
Related Questions
why i am getting this errorr.... 1 Answer
Problem Destroying Component 0 Answers
Gameobject Renderer not working. 1 Answer