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 OctoMan · Jun 23, 2015 at 11:23 PM · gameobjectdestroystoreinstantiated

Ai should delete a build turret

The AI builds turrets at a given position when he/she has enouph exp.

 if(GM.enemy_exp >= 600)
         {
             if(BuildSpots[0].tag == "TurretSpotOpen")
             {
                 Instantiate(Turrets[0], BuildSpots[0].position, Quaternion.identity);
                 BuildSpots[0].tag = "TurretSpotClosed";
             }
         }

Now at another state i want the AI to delete that turret, to make space for a new one how could i achieve this? I mean, how do i get the right object to destroy? How can i store the already build object to get rid of it later?

I bet it's super simple, but sometimes i'm blind.

Thanks in advance

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Cherno · Jun 23, 2015 at 11:39 PM

You could have a List of the turrets that were built, like this:

 using System.Collection.Generic;
 
 private List<GameObject> builtTurrets = new List<GameObject>();

You can make the Instantiate function return the gameobject that it created, so you can add this GO into the turret list.

 gameObject newturret = Instantiate(Turrets[0], BuildSpots[0].position, Quaternion.identity) as GameObject;
 
 builtTurrets.Add(newturret);
 

To destroy the turrets, you just access the references to them stored in the list. How you differentiate between turrets is up to you; Give them a unique ID in one of their scripts, change their name and add a number to it, etc. You can then, if you don't know the turret's index in the list, iterate through the list and check if the currently iterated turret's ID or whatever corresponds to the one you are searching for, and then destroy that turret (don't forget to remove it from the list first!).

Comment
Add comment · Show 4 · Share
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
avatar image OctoMan · Jun 23, 2015 at 11:50 PM 0
Share

Thanks you showed me the way i wanted it to be, kind of.

That's the result i wanted to achieve.

 if(G$$anonymous$$.enemy_exp >= 700)
         {
             if(BuildSpots[0].tag == "TurretSpotClosed")
             {
                 Destroy(OBJ001);
                 BuildSpots[0].tag = "TurretSpotOpen";
             }
         }
         else if(G$$anonymous$$.enemy_exp >= 600)
         {
             if(BuildSpots[0].tag == "TurretSpotOpen")
             {
 
                 OBJ001 = Instantiate(Turrets[0], BuildSpots[0].position, Quaternion.identity)as GameObject;
                 BuildSpots[0].tag = "TurretSpotClosed";
             }
         }

I only have 4 buildspots and not all are the whole time active, so this works for all 4 later on, i hope. :P

avatar image Cherno · Jun 23, 2015 at 11:56 PM 0
Share

In that case, a generic array might be better, because it will stay at the same size wehter it has elements in it or not. So you could have an array with a Length of 4, and each position inside the array corresponds to one of the four buildings spots. That'll make it easy to directly access one of them, because you probably will know the number of the spot.

avatar image OctoMan · Jun 24, 2015 at 12:11 AM 0
Share

Yep i will know the number, since i need to plan the AI action ahead.

avatar image OctoMan · Jun 24, 2015 at 12:41 AM 0
Share

The wierd thing when i do it like that the 2nd gun is not acting. Any ideas why?

when i place a 2nd gun on the spot i mean.

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

22 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Store a Copy then Destroy Scene Objects 1 Answer

Deleting instantiated prefabs clones? 2 Answers

Problem with timed code-execution (audioplay and object destruction) 1 Answer

Trying to make a simple inventory: 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