Problems with gameObject

private int startingAmmo;

private int currentAmmo = 10;

private int maximumAmmo;

private int newWeaponIndex;

private void Start()
{
   //Just a test, works as it should
    //this.gameObject.GetComponent<WeaponController>().newWeaponIndex = 7;
    //Debug.Log("newWeaponIndex = " + newWeaponIndex);

    //Here is the problem, none of the 5 if tests ever get run even I have copied and pasted
    //and checked very closely that the names are correct.
    if(this.gameObject.name == "Weapon_Blaster")
    {
        Debug.Log("Weapon_Blaster if test kjørte");
        this.gameObject.GetComponent<WeaponController>().startingAmmo = 25;
        
        this.gameObject.GetComponent<WeaponController>().maximumAmmo = 1000;
        
        this.gameObject.GetComponent<WeaponController>().currentAmmo =
            this.gameObject.GetComponent<WeaponController>().startingAmmo;

    } else if (this.gameObject.name == "Rocket_Launcher")
    {
        Debug.Log("Rocket_Launcher if test kjørte");
        this.gameObject.GetComponent<WeaponController>().startingAmmo = 10;

        this.gameObject.GetComponent<WeaponController>().maximumAmmo = 100;

        this.gameObject.GetComponent<WeaponController>().currentAmmo =
            this.gameObject.GetComponent<WeaponController>().startingAmmo;

    } else if(this.gameObject.name == "Ally_Healing_Bot")
    {
        Debug.Log("Ally_Healing_Bot if test kjørte");
        this.gameObject.GetComponent<WeaponController>().currentAmmo = 1000000000;

        } else if(this.gameObject.name == "Enemy_Turret")
    {
        Debug.Log("Enemy_Turret if test kjørte");
        this.gameObject.GetComponent<WeaponController>().currentAmmo = 1000000000;

        } else if(this.gameObject.name == "Enemy_HoverBot")
    {
        Debug.Log("Enemy_HoverBot if test kjørte");
        this.gameObject.GetComponent<WeaponController>().currentAmmo = 1000000000;
    }

The test at the start indicates that whats inside the if statements is correct, however the if tests is never true and therefore the code is never run. Can someone please tell me why and how to fix the this.gameObject.name == problem and if the code inside really is correct. (The this. may not be necessary, but I keep it for now to be sure the code is correct.) Thanks.

Hello. Some things…

As you say, the “this” is 100% unnecessary. The variable gameObject always refear to the object containing that script instance.

Show us the inspector. If any if is triggering, is beacause actually, the name of the object containing the script does not match (not its parent or child, only exct object).

You can always use this:

if (gameObject.name.Contains("Weapon_Blaster") )

You can DEBUG the CODEEEE (Always this must be your 1st troubleshooting method!!!) also can add a debug.Log to know what name is trying to be checked

Debug.Log(gameObject.name);

if(this.gameObject.name == "Weapon_Blaster")
     {
    [....]

Bye!

@hemingso

You might try string.Equals instead of == for the comparison… Or calling the ToString() method on GameObject.name.

Here’s a relevant discussion that took place at stackoverflow.com:

https://stackoverflow.com/questions/3678792/are-string-equals-and-operator-really-same

Thanks for your answer @tormentoarmagedoom, but I find one thing a little strange, maybe you can explain. I used Debug.Log(gameObject.name); to find the correct names which was child of the original gameObject not the original gameObject and I think that solved the problem in Start() I had to add “(Clone)” to some of them, but after that it seems to work. What I found strange is that following this way of thinking I would think that any reference to this script including, Awake(), OnEnable(), Start(), OnDisable(), calls from other scripts to reference variables in this script as in
gameObject.GetComponent?WeaponController?().newWeaponIndex = 7;, and similar calls from other scripts to reference methods from other scripts like in
FindObjectOfType?WeaponController?().IncreaseAmmo(ammoAmount);,
When used in Start(), the reference is called many times, one for each gameObject which has the script attached, however when I try the Debug.Log(gameObject.name); in the start of the method which get called from another script only one weapon shows up, and it seems completely random which (always the same weapon however), I find it strange that Start(), and a method call acts that differently, and when I want to increase the ammunition of several different weapons using one method call, how do I do that when only one gameObject gets called.
My base code is this.

public void IncreaseAmmo(int ammoAmount)
{

    Debug.Log(gameObject.name);

    if (gameObject.name == "Weapon_Blaster(Clone)")
    {
        //Debug.Log("Før WB = " + gameObject.GetComponent<WeaponController>().currentAmmo);
        gameObject.GetComponent<WeaponController>().currentAmmo += ammoAmount;
        //Debug.Log("Etter WB = " + gameObject.GetComponent<WeaponController>().currentAmmo);
    }
      //this gameObject gets called with Debug.Log(gameObject.name);
    else if (gameObject.name == "Rocket_Launcher(Clone)")
    {
        //Debug.Log("Før RL = " + gameObject.GetComponent<WeaponController>().currentAmmo);
        gameObject.GetComponent<WeaponController>().currentAmmo += (ammoAmount / 2);
        //Debug.Log("Etter RL = " + gameObject.GetComponent<WeaponController>().currentAmmo);
    }
    else if (gameObject.name == "Weapon_Shotgun")
    {
        //Debug.Log("Før WS = " + gameObject.GetComponent<WeaponController>().currentAmmo);
        gameObject.GetComponent<WeaponController>().currentAmmo += ammoAmount * 10;
        //Debug.Log("Etter WS = " + gameObject.GetComponent<WeaponController>().currentAmmo);
    }
    else if (gameObject.name == "Weapon_Launcher")
    {
        //Debug.Log("Før WL = " + gameObject.GetComponent<WeaponController>().currentAmmo);
        gameObject.GetComponent<WeaponController>().currentAmmo += ammoAmount / 2;
        //Debug.Log("Etter WL = " + gameObject.GetComponent<WeaponController>().currentAmmo);

    }

Thanks for your answers!

Use string.Equals() function instead of ==

PS: Use a switch statement instead of soo many if statements. For each case, have a separate method:

switch (name)
{
    case "Weapon_Blaster":
        WpnBlaster();    // all your code in that if statement goes in this function
        break;
    case "Rocket_Launcher":
        RocketLauncher();    // all your code in that if statement goes in this function
        break;
}
    // and so on. Instead of this.gameObject.GetComponent<WeaponController>(),
    // use GetComponent<WeaponController>().      simple :D