So I have a menu scene in which I am outfitting a spaceship with various equipment. Every time I choose a component to install on the ship, it gets added to an array in a game manager script. When I click the “Build Ship” button, OnBuildShip() is called, and it loads the next scene in which the ship is actually built.
But I need a way for the game to prevent going to the next scene if the ship does not have certain required components. I wrote this bool function that runs through the array, and checks if the current component being checked matches one of the required ones. If so, a temporary bool variable for that requirement is marked as true.
If all three of those temporary bools are true, then the function returns true. Otherwise it returns false.
Then, in OnBuildShip(), if that bool function returns true, the next scene is loaded. If not, then there is a message saying the ship does not have required components.
The problem I’m having is that the next scene loads every time, whether the required components are chosen or not. I put Debug.Log messages in the bool function to see what is being called, and none of them show up in the console. I’m really not sure what I’m doing incorrectly. Any help you can offer would be appreciated. Thanks.
Here is the relevant code:
public bool HasRequiredComponents()
{
bool hasEngine = false;
bool hasPower = false;
bool hasCooling = false;
Debug.Log("HasRequiredComponents being called");
for (int i = 0; i < gm.currentShipClass.shipComponents.Length; i++)
{
if (gm.currentShipClass.shipComponents *== engineRequirement)*
{
hasEngine = true;
Debug.Log(“hasEngine is true”);
}
if (gm.currentShipClass.shipComponents == powerRequirement)
{
hasPower = true;
Debug.Log(“hasPower is true”);
}
if (gm.currentShipClass.shipComponents == coolingRequirement)
{
hasCooling = true;
Debug.Log(“hasCooling is true”);
}
}
if (hasEngine && hasPower && hasCooling)
return true;
else
return false;
}
public void OnBuildShip() // When the BUILD SHIP button is pressed…
{
if (HasRequiredComponents())
{
SceneManager.LoadScene(“SampleScene”); // Load the next scene
}
else
{
Debug.Log(“Ship does not have required components.”);
}
}