Author Topic: Tower Defense Tower Spawning  (Read 1607 times)

Demigoth

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 18
    • View Profile
Tower Defense Tower Spawning
« on: March 15, 2014, 05:32:11 PM »
Hey ArenMook,

I was wondering if you could give me a hand on this, I tried it already by myself and somehow I cant get the object to instantiate correctly, first lets say I am making a tower defense game, and I am using both NGUI and HUD TEXT extension. Second point, I have the places where I can spawn the towers so when I click on them I get a NGUI menu to popup and I select the tower and it spawns it.
Now I am facing 2 problems here. First, the menu is a custom panel with 4 buttons, and I deactivate it, so I called it from a script on the towerslot to make it appear, there are several towerslots on the game so I want the panel to appear on top of any when I click on them, I tried using the UIFollowTarget but it doesnt work correctly with all the prefabs of the towerslots.

Second, when I click on the tower I want to instantiate, I get this code to do it but it doesn't instantiate on the towerslot, it does instantiate on the UI object location. Any advice would be great, I was trying to store the towerslot position inside a variable but that doesn't work or I might be doing it wrong.

This is the code for the Tower instantiation...

  1. public GameObject archerTower = null;
  2.         // Use this for initialization
  3.         void OnClick ()
  4.         {
  5.                 if (Player.gold >= ArcherTowerStats.towerCost)
  6.                 {
  7.                         Player.gold -= ArcherTowerStats.towerCost;
  8.  
  9.                         Instantiate (archerTower, transform.position,  Quaternion.identity);
  10.  
  11.                         gameObject.SetActive(false);
  12.                 }
  13.         }
  14.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tower Defense Tower Spawning
« Reply #1 on: March 15, 2014, 06:27:55 PM »
Well yeah, you use transform.position, where 'transform' is your OnClick object, so your UI object.

If you want to instantiate it in the world, then use your world coordinates there. Kind of self-explanatory there...

UIFollowTarget is a script you can use to have a UI object follow a 3D object. For it to work you need to attach it to your UI object and set its target, with the target being your 3D object. Furthermore, the camera that draws your UI must not draw the target object, and similarly -- camera that draws your target object must not draw your UI. You can set the camera on UIFollowTarget explicitly as well -- which would be best.