Having trouble getting a bool function to work

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

Hey there,

i don’t think this can be completly answered with the information given.
However let’s try:
Now the actual question here is: What is the type of “shipComponents” and what do you assign in it?

Because i suspect that you, for example assign a copy of a prefab. A comparison between this and the original will not result in true. I have a similar system in my game where you build a weapon from different parts. To solve this issue i created a baseclass “WeaponPart” and my List of Parts is a

  List<WeaponPart> AllWeaponParts = new List<WeaponPart>();

Then i have an enumeration:

 public enum WeaponPartTypes { Base, Trigger, Barrel, Magazin}

and my class has a variable of that type:

 public class WeaponPart : MonoBehaviour{
         public WeaponPartTypes PartType;
 }

Now what i can do is to check if a weapon part is of a certain type:

 if(AllWeaponParts*.PartType == WeaponPartTypes.Trigger) //this is true if the part is a trigger*

Another good way to do this would be if i have an inherited class from the “WeaponPart”. Lets assume my Trigger has its own class:
public class WeaponTrigger : WeaponPart //we inherit from WeaponPart
This way we can add WeaponTrigger as a Script to a gameObject, but at the same time we can still assign it to our AllWeaponParts List as each WeaponTrigger is also of Type WeaponPart.
This now allows us to check for:
if(AllWeaponParts is WeaponTrigger) //check if the Type of our WeaponPart is a WeaponTrigger
This should be 100% applicable for your case and should solve the issue if you apply this. Just replace everything that is “Weapon” by “Ship” and instead of Trigger, Barrel, Magazine you have Power, Cooling, Engine.
I can strongly recommend to go the inheritance route here as this is in general good practice in Object Oriented Programming.
Hope this helps, let me know if something is unclear.

“What is the type of “shipComponents” and what do you assign in it?”

shipComponents is an array of scriptable objects. In the following scene, there is a function that instantiates a prefab that is referenced in the scriptable object.
As far as I know, there shouldn’t be a problem with comparing a specific instance of a scriptable object to itself. But even if there was, then the bool function should be returning false. But it appears, based on what happens in OnBuildShip(), to be returning true.

Oh boy, do I feel stupid…
In the button itself, the button is linked to a different function in a different version of this script. I linked it to the correct function and it works perfectly.