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 TargonStudios · Nov 06, 2013 at 01:32 PM ·

Counting number of blocks in an area

Title sums it up. I have been working on random generation lately. I have made it so when water is generated, there is a chance that a "water checker" is spawned. What I want the water checker to do is count the amount of water in the area to see if it is suitable for fish to spawn. Now I know how to spawn fish and all, but how would I start at a script that finds blocks with the name/Tag "Water" in a certain Radius and count them? I'm not asking anyone to write me a script, I want to learn how to do this for other things I'm going to make for random generation. Maybe if there's a link, or steer me in the right direction. thanks.

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 TheValar · Nov 06, 2013 at 01:43 PM 0
Share

if your water tiles have colliders you could use an overlap sphere. http://docs.unity3d.com/Documentation/ScriptReference/Physics.OverlapSphere.html

then check all the hitColliders for the tag and increment a counter when one is found.

p.s. not posting this as an answer because I'm not sure if your tiles have colliders :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LiverX! · Nov 06, 2013 at 01:58 PM

Create new array:

C# - GameObject[] array = GameObject.FindGameObjectsWithTag("Water");

JS - var array : GameObject = GameObject.FindGameObjectsWithTag("Water");

And using Vector3.Distance(a,b) counting the objects which is distance from "mark" is not greater than some number.

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 TargonStudios · Nov 07, 2013 at 12:10 AM 0
Share

So I have got It to count the water blocks in the game, and display the number for me to see, but I need help on the distance thing. Here is my script so far.

 var array : Array; 
 var NumberOfWaterBlocks = 0;
 
 function Update(){ 
 array = GameObject.FindGameObjectsWithTag("Water"); print(array); NumberOfWaterBlocks = array.length;
 
 }

Now how would I be able to incorporate Distance? Ive tried some things but I don't know how to $$anonymous$$us the number if it is out of range, etc.... I don't even know where to begin.

avatar image Huacanacha · Nov 07, 2013 at 12:24 AM 0
Share

So you can get the list or relevant game objects (per your code above), now you just need to count all of those objects within a certain distance from your spawnPosition... to calculate the distance:

 Vector3.Distance(spawnPosition, waterObject.transform.position);

If you were using C# I would include the code for doing the actual counting, but I don't know the JS collection methods.

avatar image TargonStudios · Nov 07, 2013 at 12:57 AM 0
Share

But what would "waterObject" Be?

avatar image Huacanacha · Nov 07, 2013 at 01:08 AM 0
Share

Ok here it is in C#... hopefully this helps you understand what I mean:

 GameObject.FindGameObjectsWithTag("Water").Count(waterObject => Vector3.Distance(spawnPosition, waterObject.transform.position) < spawnRadius);

You would need to include System.Linq. Spawn position is hopefully self explanatory, and spawnRadius is the max distance from spawnPoint to count the water object.

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

18 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

Related Questions

Alternative To "SetEditorCurve"? 1 Answer

input.getaxis not working 0 Answers

Health not counting down? 1 Answer

Can't call a function from another script 1 Answer

Is there a way to activate a boolean through tag? 1 Answer


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