Calling all conditions - why?

For some reason all three if statements are being called when the conditions are different.

Above you can see where Mounted Turret has the object of HomingTurret, and in the code I have -

public void DestroyTurret()
    {
        if(buildPoint.mountedTurret = fixedTurret)
        {
            Debug.Log("FixedTurret Destroyed");
            //Other stuff
        }

        if(buildPoint.mountedTurret = homingTurret)
        {
            Debug.Log("HomingTurret Destroyed");
           //Other stuff
        }

        if(buildPoint.mountedTurret = scanningTurret)
        {
            Debug.Log("ScanningTurret Destroyed");
           //Other stuff
        }
    }

Every if Statement gets called regardless of which object is the Mounted Turret.
I have also tried using if(buildPoint.mountedTurret = fixedTurret.gameObject) and if(buildPoint.mountedTurret.Equals(“HomingTurret(Clone)”)) etc.

Some help would be appreciated.

You sure you don’t want to use == instead of =?

        if(buildPoint.mountedTurret == fixedTurret)

Is valid, but then nothing is being called. I’m not sure why, as I can instantiate them with the names fixedTurret, homingTurret, scanningTurret, but for some reason it doesn’t seem that I can access them here.

What he’s saying there is that all 3 were being called because you were assigning them in each if statement, you weren’t asking if they were true or not. Now you’ve changed it you know none of them are being called. So instead you want to do a Debug.Log to see what IS being called. In the script type:

Debug.Log("buildPoint.mountedTurret = " + buildPoint.mountedTurret);

It all depends on what

if(buildPoint.mountedTurret.tag == "SomeTagName")
// Do something cool!!!

is actually referring to. What is fixedTurret? Is it a gameObject? A string? A tag? A script? Normally people compare objects not by ‘is this the exact same instance of an object?’, but by asking ‘is this object of this type?’. You could give each turret a unique tag, which denotes their type, and see if by comparing tags, they’re the same.

So you’d assign the fixed turret the tag ‘FixedTurret’, or something like that, then check in script whether the object has that type of tag:

1 Like

Thank you, I forgot all about tags. That’s just what was needed.