Need help in Realm rush Tower instantiate and destruction

I am trying to make my tower destroyable after some time using coroutine, but the place where I put the tower, I can’t put the tower anymore even after the tower is destroyed

Hi Jatin,

The Waypoint contains a bool which tells the tower factory/spawner whether it is occupied or not. When destroying the tower, you need to tell the Waypoint that a new tower could be placed here.

Hi Nina,
I tried but unity is showing this message- Destroying assets is not permitted to avoid data loss.
I tried to destroy the tower after 15 seconds for that I am using coroutine like this-
Here towerHealth =2;
IIEnumerator destroyTowers()
{
yield return new WaitForSeconds(15);
towerHealth–;
if (towerHealth <= 1 )
{
Destroy(gameObject);
}
}

Check if your code references a prefab. Prefabs must not get destroyed via code.

Ok let me try again

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Maybe the relevant if-blocks do not get called.


See also:

Hi Nina,
I am trying to make this project for my portfolio so I am trying to make some changes,
The enemy follows the same path every time so,

I have three ideas to stop this -

  1. I try to generate a tree randomly in the map,
  2. The direction search changes automatically,
  3. Player destroy itself after some time,

Now Problems are -

  1. I am instantiating trees and giving them random positions using Random. Range, But the problem is Tiles don’t know where is the tree is placed I know there is bool is Placed but, the tree is instantiated randomly in the map it does not know about the grid and, I don’t know how can I pass the value to that bool so, tiles know where is a tree.

  2. I don’t know anything about the direction changing is there any method or function like Random. Range.

  3. Here this problem is similar to the 1st one like when I destroy the tower I don’t know whom I have to tell tower is destroyed like there many bools - isSuccessful, is placed, iswalkable, I think all the bools need information about the tower destruction, and I don’t know how can I do this.

So tell me what should I do, should I skip these ideas now and proceed to the next lecture.
(English is not my native language, so if you didn’t understand what I want to say then, I am sorry I tried my best to tell you the problem I am facing).

Your ideas sound realisable. At least, you have all the knowledge you need to implement them in Unity. You just need a concept. Use pen and paper to draw the logic. And don’t forget: Gary’s solution solves Gary’s problems, not necessarily yours. If necessary, don’t hesistate to replace his code.

And before you make any bigger changes, make a backup of your project folder by duplicating it. Save the duplicated folder somewhere else, maybe on an USB stick. To save some free space, you could delete the Library and Temp folders in the backup folder. This way, there is nothing to worry about if you accidentally break your project.

#1: At the moment, the waypoint knows if it is occupied. You could treat towers and trees the same way. However, depending on the other mechanics in the game, two bools might make more sense: one for the tower, one for the trees. If you want to pass on data to another object (or method), use a technique called “method overload”:
https://www.dotnetperls.com/overload

Is there a reason why you don’t use the waypoint grid to instantiate the trees?

#2: Yes, there is a Random.Range method in Unity. Check the API.

#3: The waypoint does not know anything about towers (hopefully). It only knows if it’s occupied or not, see isPlaced.


Thinking about your ideas, I would like to share a slightly different approach for a solution with you. This will require you to drop a part of Gary’s solution but could make your game more flexible in case you will come up with more ideas.

  • Do not instantiate anything directly. Instead, create a new Dictionary which contains information on the relevant positions in your game and the “state” of that position. This dictionary will be the source of information for your game play. (We did something similar in the GridManager.)

  • Create an enum with three items: None, Tree, Tower. Look enums up on the internet.

  • In the aforementioned dictionary, you use the position as the key and “None” for the value.

  • In your TreeSpawner class (or wherever you spawn the trees), you check the dictionary before spawning a tower. If the dictionary contains the position, you set the value to tree.

  • When you run your pathfinding algorithm, the algorithm checks the dictionary. If the value of the current position is not “None”, the waypoint must not be included in the path.

Theoretically, you could design your entire game on this abstract level with the dictionary and generate your game objects during runtime based on the information in the dictionary.

That’s just an idea, though, and probably everything but perfect. Nevertheless, it could help you anyway, and perhaps you’ll come up with an even better solution. :slight_smile:

Thank you for this supportive replay Nina, I will try all the ways you mentioned above and will contact you if I face any issue/problem.
Thank you again and I am glad I asked the question at the right place.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms