- Home /
Help with an upgrade tower system
I am having trouble getting a UI button to show up when I use on mouse enter once a tower is placed to upgrade it when I have enough funds. I just want to be able to click the green button and Instantiate the tower upgrade at the selected tower Any help or tips would be great.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.EventSystems;
public class PlacementScript : MonoBehaviour
{
ParticleSystem _part;
GameObject _tower;
WarFunds war;
bool _spotAv = true;
bool _upgrade= false;
BuildManager build;
public static Action canUpgradeGat;
public int Towernum;
UI_Manager ui;
// Start is called before the first frame update
void Start()
{
war = WarFunds.Instance;
build = BuildManager.Instance;
_part = GetComponent<ParticleSystem>();
ui = GetComponent<UI_Manager>();
}
// Update is called once per frame
private void OnMouseDown()
{//only place if no tower here
if (_spotAv == true && build._towerDecoys[0].activeInHierarchy == true || build._towerDecoys[1].activeInHierarchy == true)
{
build.PlaceTower(this.transform.position);
_spotAv = false;
_upgrade = true;
build.TowerOff();
if(build._currentTowerIndex == 0)
{
Towernum = 0;
}
if (build._currentTowerIndex == 1)
{
Towernum = 1;
}
}
else if (_spotAv == false)
build.OffRadiusColor();
}
private void OnMouseEnter()
{
build.SnapTower(this.transform.position);
build.UpdateRadiusColor();
_part.Play(true);
if (_spotAv == false)
{
build.OffRadiusColor();
}
if (_upgrade == true && war.CurrentWarFunds > 500)
{
canUpgradeGat?.Invoke();
}
}
private void OnMouseExit()
{
_part.Stop(true);
build.SnapTower(transform.position, true);
// give control back to towermanager
// turn radius red
build.OffRadiusColor();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuildManager : MonoBehaviour
{
private static BuildManager _instance;
public static BuildManager Instance
{
get
{
if (_instance == null)
{
Debug.LogError("Build Manager is NULL");
}
return _instance;
}
}
private void Awake()
{
_instance = this;
}
public GameObject[] _towerDecoys;
[SerializeField]
public GameObject[] _towersToPlace;
public GameObject[] _upgradesToPlace;
public int _currentTowerIndex;// 0 = gat & 1 = missle
public int _currentTowerDecoys;// 0 = gat & 1 = missle
bool _placingTower;
ITower Funds;
WarFunds war;
private void Start()
{
war = WarFunds.Instance;
}
private void Update()
{
if(_placingTower == true)
{
Ray rayorigin = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitinfo;
if (Physics.Raycast(rayorigin, out hitinfo))
{
_towerDecoys[_currentTowerIndex].transform.position = hitinfo.point;
}
}
if (Input.GetMouseButtonDown(1))
{
_towerDecoys[_currentTowerIndex].SetActive(false);
}
}
public void SetTower(int tower)
{
_currentTowerIndex = tower;
_towerDecoys[_currentTowerIndex].SetActive(true);
_placingTower = true;
}
public void SnapTower(Vector3 pos, bool followmouse = false)
{
_towerDecoys[_currentTowerIndex].transform.position = pos;
_placingTower = followmouse;
}
public void UpdateRadiusColor()
{
_towerDecoys[_currentTowerIndex].GetComponent<ParticleSystem>().startColor = Color.green;
}
public void OffRadiusColor()
{
_towerDecoys[_currentTowerIndex].GetComponent<ParticleSystem>().startColor = Color.red;
}
public void PlaceTower(Vector3 pos)
{
if(_currentTowerIndex == 0)
{
war.CurrentWarFunds -= 50;
}
if (_currentTowerIndex == 1)
{
war.CurrentWarFunds -= 100;
}
Instantiate(_towersToPlace[_currentTowerIndex], pos, Quaternion.identity);
}
public void TowerOff()
{
_towerDecoys[_currentTowerIndex].SetActive(false);
}
public void UpGradeTowerGat(Vector3 pos)
{
if (_currentTowerIndex == 0 && war.CurrentWarFunds > 100 )
{
war.CurrentWarFunds -= 100;
}
if (_currentTowerIndex == 1 && war.CurrentWarFunds > 150)
{
war.CurrentWarFunds -= 150;
}
Instantiate(_upgradesToPlace[0], pos, Quaternion.identity);
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class UI_Manager : MonoBehaviour { [SerializeField] Text _wartext;
BuildManager build;
[SerializeField]
WarFunds war;
[SerializeField]
GameObject upgradeGat;
[SerializeField]
GameObject upgradeMissile;
PlacementScript placenum;
// Start is called before the first frame update
void Start()
{
war = WarFunds.Instance;
_wartext.text = "$" + war.CurrentWarFunds;
build = BuildManager.Instance;
placenum = GetComponent<PlacementScript>();
}
// Update is called once per frame
void Update()
{
_wartext.text = "" + war.CurrentWarFunds;
}
public void GatlingGun()
{// check warfunds amount
if (war.CurrentWarFunds < 50)
{
Debug.Log("Need more money");
return;
}
else
build.SetTower(0);
}
public void Missle()
{
if (war.CurrentWarFunds < 100)
{
Debug.Log("Need more money");
return;
}
else
{
// check warfunds amount
build.SetTower(1);
}
}
public void UpgradeGat()
{
Debug.Log("Working");
StartCoroutine(UnHideGat());
}
public IEnumerator UnHideGat()
{
{
upgradeGat.SetActive(true);
yield return new WaitForSeconds(5);
upgradeGat.SetActive(false);
}
}
private void OnEnable()
{
PlacementScript.canUpgradeGat += UpgradeGat;
}
private void OnDisable()
{
PlacementScript.canUpgradeGat -= UpgradeGat;
}
}
Your answer
Follow this Question
Related Questions
Passing through a GameObject/Function to a button's OnClick 1 Answer
Unity 5.0.2f1 UI Button OnClick Function 6 Answers
Change Scroll bar on click 0 Answers
How to make a custom onClick event? 2 Answers
How do change UI.button onclick states(off, editor and runtime, runtime only) via script? 0 Answers