Tuturial 19 boost: Make2 Turret upgrades instead of 1

Updated on July 3, 2018 in [A] Tutorials
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on July 3, 2018

Edit:
At this moment I have 3 buttons at any time. Upgrade1, Upgrade2 and Sell. I managed to get only Upgrade1 or 2 to work at the same time and also accros all the towers. Nothing is disabled I used interactable instead on the switches. Its an oke fix for now, but I will still love to have a fix where you can only see 1 local (when klicking on Turret 1) Upgrade option at the time, and making sure there is a check wether this tower is on Upgrade 1 or 2.

Using interactable seem to be localy on the target whereass enabled flips the switch in the entire game.

 if (!target.isUpgraded)
 {
 upgradeCost.text = "$" + target.turretBlueprint.upgradeCost;
 upgradeButton.interactable = true;
 upgradeButton2.interactable = false;
 } 

Could anyone please explain me how I can unshow/disable 1 of the 2 upgradeButtons? Both are disabled on startup in the Canvas Hierarchy

Or
Is there a posibility to convert/transform the UpgradeButton 1 to UpgradeButton 2? That would also solve the problem I think. Its either upgrade or sell, never downgrade.

———————————
Original post
Hey there,

I’ve been watching tutorial 19 a few times now but I can’t seem to figuer out why my upgraded turret won’t upgrade a 2nd time. I have the Debug.Log still active and all I keep seeing is “Turret upgraded”. I don’t get any errors, but for some reason it simply isn’t activating.

 

I have also tried to make an additional Button, so that I have a “UpgradeButton” + “UpgradeButton2”. I don’t understand how I can localy have UpgradeButton on and not UpgradeButton2 or vise versa. Via the NodeUI in the hierarchy I can disable it for all turrets and not just the 1 selected. So I know it has to be added in the script NodeUI but I don’t know how.
Please can anyone give me some help on to fix this via 1 of the 2 options. Or a 3th if thats easyer. I have followed all Tuts up to this and everything works flawless.

 

I will include the modified scripts.

 

TurretBlueprint.cs

 
using UnityEngine;
using System.Collections;
 
[System.Serializable]
public class TurretBlueprint {
 
public GameObject prefab;
public int cost;
 
//Upgrade 1
public GameObject upgradedPrefab;
public int upgradeCost;
 
//Upgrade 2
public GameObject upgradedPrefab2;
public int upgradeCost2;
}

 

NodeUI.cs

 
using UnityEngine;
using UnityEngine.UI;
 
public class NodeUI : MonoBehaviour {
 
public GameObject ui;
 
public Text upgradeCost;
public Button upgradeButton;
 
public Text upgradeCost2;
public Button upgradeButton2;
 
private Nodes target;
 
public void SetTarget (Nodes _target)
{
target = _target;
 
transform.position = target.GetBuildPosition();
 
if (!target.isUpgraded)
{
upgradeCost.text = "$" + target.turretBlueprint.upgradeCost;
upgradeButton.interactable = true;
}
 
if (target.isUpgraded2)
{
upgradeCost.text = "$" + target.turretBlueprint2.upgradeCost2;
upgradeButton2.interactable = true;
// upgrade2Button.enabled = true;
}
 
if (target.isMaxed)
{
upgradeCost.text = "Maxed out";
upgradeButton.interactable = false;
}
 
ui.SetActive(true);
}
 
public void Hide ()
{
ui.SetActive(false);
}
 
public void Upgrade()
{
target.UpgradeTurret();
BuildManager.instance.DeselectNodes();
}
 
public void Upgrade2()
{
target.UpgradeTurret2();
BuildManager.instance.DeselectNodes();
}
 
}

 

Nodes.cs (I renamed this instead of Node I took Nodes)

 
using UnityEngine;
using UnityEngine.EventSystems;
 
public class Nodes : MonoBehaviour {
 
public Color hoverColor;
public Color notEnoughMoneyColor;
public Vector3 positionOffset;
 
[HideInInspector]
public GameObject turret;
[HideInInspector]
public TurretBlueprint turretBlueprint;
[HideInInspector]
public TurretBlueprint turretBlueprint2;
[HideInInspector]
public bool isUpgraded = false;
[HideInInspector]
public bool isUpgraded2 = false;
[HideInInspector]
public bool isMaxed = false;
 
private Renderer rend;
private Color startColor;
 
BuildManager buildManager;
 
void Start ()
{
rend = GetComponent();
startColor = rend.material.color;
 
buildManager = BuildManager.instance;
}
 
public Vector3 GetBuildPosition ()
{
return transform.position + positionOffset;
}
 
void OnMouseDown ()
{
if (EventSystem.current.IsPointerOverGameObject())
return;
 
if (turret != null)
{
buildManager.SelectNode(this);
return;
}
 
if (!buildManager.CanBuild)
return;
 
BuildTurret(buildManager.GetTurretToBuild());
}
 
void BuildTurret (TurretBlueprint blueprint)
{
if (PlayerStats.Money < blueprint.cost)
{
Debug.Log("Not enough money!");
return;
}
 
PlayerStats.Money -= blueprint.cost;
 
GameObject _turret = (GameObject)Instantiate(blueprint.prefab, GetBuildPosition(), Quaternion.identity);
turret = _turret;
 
turretBlueprint = blueprint;
turretBlueprint2 = blueprint;
 
GameObject effect = (GameObject)Instantiate(buildManager.buildEffect, GetBuildPosition(), Quaternion.identity);
Destroy(effect, 5f);
 
Debug.Log("Turret build!");
}
 
public void UpgradeTurret()
{
if (PlayerStats.Money < turretBlueprint.upgradeCost)
{
Debug.Log("Not enough money to upgrade!");
return;
}
 
PlayerStats.Money -= turretBlueprint.upgradeCost;
 
//Destroy the old turret
Destroy(turret);
 
//Build a new one
GameObject _turret = (GameObject)Instantiate(turretBlueprint.upgradedPrefab, GetBuildPosition(), Quaternion.identity);
turret = _turret;
 
GameObject effect = (GameObject)Instantiate(buildManager.upgradeEffect, GetBuildPosition(), Quaternion.identity);
Destroy(effect, 5f);
 
isUpgraded = true;
isUpgraded2 = true;
 
Debug.Log("Turret upgraded 1!");
}
 
public void UpgradeTurret2()
{
if (PlayerStats.Money < turretBlueprint2.upgradeCost2)
{
Debug.Log("Not enough money to upgrade 2!");
return;
}
 
PlayerStats.Money -= turretBlueprint2.upgradeCost2;
 
//Destroy the old turret
Destroy(turret);
 
//Build a new one
GameObject _turret = (GameObject)Instantiate(turretBlueprint2.upgradedPrefab2, GetBuildPosition(), Quaternion.identity);
turret = _turret;
 
GameObject effect = (GameObject)Instantiate(buildManager.upgradeEffect, GetBuildPosition(), Quaternion.identity);
Destroy(effect, 5f);
 
isMaxed = true;
 
Debug.Log("Turret upgraded 2!");
}
 
void OnMouseEnter ()
{
if (EventSystem.current.IsPointerOverGameObject())
return;
 
if (!buildManager.CanBuild)
return;
 
if (buildManager.HasMoney)
{
rend.material.color = hoverColor;
}
else
{
rend.material.color = notEnoughMoneyColor;
}
 
}
 
void OnMouseExit ()
{
rend.material.color = startColor;
}
 
}

 

BuildManager.cs

 
using UnityEngine;
 
public class BuildManager : MonoBehaviour {
 
public static BuildManager instance;
 
void Awake()
{
if (instance != null)
{
Debug.LogError("More then one BuildManager in scene!");
return;
}
instance = this;
}
 
public GameObject buildEffect;
public GameObject upgradeEffect;
 
private TurretBlueprint turretToBuild;
private Nodes selectedNode;
 
public NodeUI nodeUI;
 
public bool CanBuild { get { return turretToBuild != null; } }
public bool HasMoney { get { return PlayerStats.Money >= turretToBuild.cost; } }
 
public void SelectNode (Nodes nodes)
{
if(selectedNode == nodes)
{
DeselectNodes();
return;
}
 
selectedNode = nodes;
turretToBuild = null;
 
nodeUI.SetTarget(nodes);
}
 
public void DeselectNodes()
{
selectedNode = null;
nodeUI.Hide();
}
 
public void SelectTurretToBuild (TurretBlueprint turret)
{
turretToBuild = turret;
DeselectNodes();
}
 
public TurretBlueprint GetTurretToBuild ()
{
return turretToBuild;
}
 
}
 

 

Thanks in advance!

  • Liked by
Reply