Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by MyNameisNonono · Apr 01, 2020 at 05:54 AM · tower-defense

Tower Defense- cannot clone the tower

Hi, I played around with Unity a month ago and now I am following the tower-defense tutorial. Those tutorials is a bit advanced to me but I feel more excited to make a real game than work with some basic course. But I stuck at the "placing tower" step.

The github link: The source

The ideal is: When I click the button, I can clone the corresponding tower and place them anywhere on the map. alt text

I have two scripts:

  • List item

TowerBtn:

 using UnityEngine;
 
 public class TowerBtn : MonoBehaviour
 {
     
     [SerializeField]
     private GameObject towerObject;
 
     public GameObject TowerObject
     {
         get
         {
             return towerObject;
         }
     }
 
 }


It attaches to the button and I use the gameObject prefab (ex : the tower) alt text

  • List item

And Tower Manager:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class TowerManager : Singletons<TowerManager>
 { 
      private TowerBtn TowerBtnPressed= null;
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Vector2 worldpoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             RaycastHit2D hit = Physics2D.Raycast(worldpoint, Vector2.zero);
             placeTower(hit);
         }
     }
 
     public void placeTower(RaycastHit2D hit)
     {
         Debug.Log(TowerBtnPressed);
         if (!GetCurrent().IsPointerOverGameObject() && TowerBtnPressed != null)
         {
             GameObject newTower = Instantiate(TowerBtnPressed.TowerObject) as GameObject;
             newTower.transform.position = hit.transform.position;
         }
     }
 
     private static EventSystem GetCurrent()
     {
         return EventSystem.current;
     }
 
     public void SelectedTower(TowerBtn towerSelected)
     {
         TowerBtnPressed = towerSelected;
         Debug.Log(TowerBtnPressed);
         Debug.Log("Press!" + TowerBtnPressed.TowerObject);
     }
 }

Every time I press Button, The SelecTower function is called.

The problem: When I click to the map to clone the tower, nothing appears also, no errors. I did some research and found out:

  1. When I click the button, the Debug.Log("Press!" +TowerBtnPressed) show that TowerBtnPressed = towerSelected. But the function placeTower() is useless since the TowerBtnPressed is always null.

alt text

  1. When I change [private TowerBtn TowerBtnPressed= null;] to [ public TowerBtn TowerBtnPressed= null;], there are only 1 type of tower is clone( For example I have 3 type of tower-Tower1,tower2,tower3, but only tower1 eventhough I click 3 buttons)

@Cunningham620 : Hi, I found out you are the instructor, could you please help me.

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

197 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

Related Questions

Unity Navigation stopping distance irregularity 1 Answer

moveTowards not working 0 Answers

how to stop spawning wave after game over 1 Answer

Custom waves is an TD game 0 Answers

Problem with pause menu/button - It doesn't pause 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