Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Buttfayce · Jul 12, 2015 at 08:28 PM · arrayrandom.range

How do I get my enemy to retarget within Random.Range?

I'm building a clone of missile command, and I'm stumped with one aspect. I have a gameobject called GameControl which fires rockets at 1 of 5 cities, so something like this:

 int selector;
 
 void FireOne(){
 
   selector = Random.Range(0,5);
 
 }

And then I have an array of gameobjects called cities, and I have the rocket target cities[selector]. My question is once a city is destroyed, how do I get the GameControl to ignore the destroyed city without removing the object from the array?

Comment
Add comment · Show 1
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 Buttfayce · Jul 13, 2015 at 12:30 AM 0
Share

Thanks for all the feedback, i ended up using the solution very similar to the one suggested by felixpk, but thank you all for helping me out! :D you rock Unity Answers!!

2 Replies

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

Answer by felixpk · Jul 12, 2015 at 09:21 PM

I think something like this would work:

  1. Add a temp List

  2. Add a parameter to the Cityobject to detect wether this city is destroyed or not:

    bool isDestroyed;

  3. Fill your temp List with just the active Cities:

      for(int i = 0; i < cities.length; i++) {
         if(!cities[i].GetComponent<CityScript>().isDestroyed) {
            tempCityList.Add(cities[i]);
         }
      }
    
    
  4. Now you have a List with only active Cities and you can select them like this:

    selector = Random.Range(0,tempCityList.Count);

    tempCityList[selector];

Comment
Add comment · 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
0

Answer by Cambesa · Jul 12, 2015 at 09:37 PM

Hey,

I would create a separate array to manage the active cities. When the game/scene starts use:

 GameObject[] activeCities;
 activeCities = GameObject.FindGameObjectsWithTag("city");

Which creates an array featuring all objects which has a tag "city".

Then when you want to fire a missile try:

 selector = Random.Range(0,activeCities.Length);
 GameObject CityToShootAt=activeCities[selector];

Finally, when a city is destroyed run:

 activeCities = GameObject.FindGameObjectsWithTag("city");

again to update the array with only the existing cities.

Hope this helps you out!

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to know what random number is chosen 2 Answers

Why is Random.Range not working in array 1 Answer

How to Show Random.Range in the Console when used in an Array? 0 Answers

pick a random int with the value of 1 from an array 2 Answers

how to have an array in instantiate function 2 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