New inistantiate Gameobject dont change variable

Hi Community,

i programming a tower defence game at the moment. Now i got different models for the towers and it all around works fine. When i replace the tower on the gamefield it works but in the script is something wrong.

I change instandly the current state of my tower to an “FrozenTower”. After that i will update the tower to level 2 and the state of the tower is always defaulttower, although I change the state to Frozetower directly under the instantiate code line. Can someone help me ?

Its the same instance id when i create it and try to change the state. I think, i dont know why, the tower dont save the state. Its always the same

public class Tower : MonoBehaviour {


    //Public variables

    public GameObject toFrozenTower;
    public GameObject toDefaultTower;
    public GameObject toStuckTower;

    private PriceManager pManager;
    private UIManager uiManager;

    //Default Values of a default tower
    protected int iTowerLevel = 1;
    protected int iRange = 50;
    protected float flShootCoolDown = 0.0f;
    protected float flShootTime = 2.0f;
    protected float flTowerDmg = 0.5f;
    protected float flStunTime = 0.2f;
    protected float flFrozeTime = 0.5f;

    //Referenz object of a default tower
    protected BuildingPlacement buBuildingPlacement;
    protected EnumCollection.TypeTower enTypeTower;

    void Awake(){
        pManager = GameObject.FindGameObjectWithTag ("GameManager").GetComponent<PriceManager> ();
        uiManager = GameObject.FindGameObjectWithTag ("GameManager").GetComponent<UIManager> ();
    }

    void Start(){
    }

    public void setPlaneUnderTower(BuildingPlacement buBuildingPlacement){
        this.buBuildingPlacement = buBuildingPlacement;
        Debug.Log (this.buBuildingPlacement.name);
        Debug.Log (this.gameObject.GetInstanceID());
    }
       
    public void UpgradeTower(string sTypeTower){

        GameObject gaNewTower = null;
        Debug.Log (this.gameObject.GetInstanceID ());
        Debug.Log (this.gameObject.GetComponent<Tower>().getTypeTower());
        switch(sTypeTower){
        case "FRTower":
            if (!(enTypeTower == EnumCollection.TypeTower.FrozenTower)) {
                gaNewTower = (GameObject)Instantiate (toFrozenTower,gameObject.transform.position,gameObject.transform.rotation);
                gaNewTower.GetComponent<Tower> ().SetTypeTower (EnumCollection.TypeTower.FrozenTower);
                Debug.Log (gaNewTower.GetInstanceID ());
           
                Destroy (this.gameObject);
            }
            enTypeTower = EnumCollection.TypeTower.FrozenTower;
            break;
        case "DFTower":
            if (!(enTypeTower == EnumCollection.TypeTower.DefaultTower)) {
                gaNewTower = (GameObject)Instantiate (toDefaultTower,gameObject.transform.position,gameObject.transform.rotation);
            }
            enTypeTower = EnumCollection.TypeTower.DefaultTower;
            break;
        case "STTower":
            if (!(enTypeTower == EnumCollection.TypeTower.StuckTower)) {
                gaNewTower = (GameObject)Instantiate (toDefaultTower,gameObject.transform.position,gameObject.transform.rotation);
                gaNewTower.GetComponent<Tower> ().SetTypeTower (EnumCollection.TypeTower.StuckTower);
            }
            enTypeTower = EnumCollection.TypeTower.StuckTower;
            break;
        }
        if (gaNewTower != null) {
            uiManager.SetCurrentTower (gaNewTower);
            pManager.upgradeTower (gaNewTower.GetComponent<Tower>(), enTypeTower);
        } else {
            pManager.upgradeTower (this, enTypeTower);
        }
    }

:smile: CLOSE i found the mistake :smile:

My mistake was pretty simple I would say.

My child class ShootTower extends from Tower. When a tower was created the start method of shoottower was initilize and in this method i got my mistaken code.

I set all Shoottower by default to DEFAULTTOWER !

void Start(){
        enTypeTower = EnumCollection.TypeTower.DefaultTower;
        trHead = gameObject.transform.FindChild ("TowerHead");
        trShootingPosition = gameObject.transform.FindChild ("TowerHead/t_ShootingPosition");
    }