Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by desonn · Jun 23, 2020 at 04:17 PM · uionclicktower-defenseonmouseover

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

220 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges