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 Blobfysh · Nov 14, 2014 at 07:33 AM · javascriptfor loopvector3.distance

findgameobjectswithtag focusing on 1 object

So I'm making a 2d game where you go around mining minerals and in order to see what's ahead you need to be next to a block to turn off a SpriteRenderer component so you can see what the block is. But when I wanted to make torches to keep the blocks uncovered even while you're not standing next to it, the blocks only uncover for 1 torch. I want it to work for multiple torches. Heres the code that goes on the mineral. I think I did the for loop wrong. Can't seem to find solution anywhere.

 var cover : GameObject; //the cover
 var mineHelm1 = false; //mining helmet upgrade to see farther
 var mineHelm2 = false; //upgrade 2
 var torchInRange = false; //turns true so that blocks ignore the player and use torch to uncover instead
 
 function Start () {
 
 
 }
 
 function Update () {
 var torches : GameObject[] = GameObject.FindGameObjectsWithTag("torch");
 var range = Vector3.Distance(this.transform.position,GameObject.Find("Player").transform.position);
 //^^ distance between this block and player
 if(range <= 2.7f && mineHelm1 == false && mineHelm2 == false && torchInRange == false){//to detect if player is close
     cover.GetComponent(SpriteRenderer).enabled = false;
     }
 else if(range <= 5.3f && mineHelm1 == true && torchInRange == false){
     cover.GetComponent(SpriteRenderer).enabled = false;
     }
 else if(range <= 7.9f && mineHelm2 == true && torchInRange == false){
     cover.GetComponent(SpriteRenderer).enabled = false;
     }
 else if(range >= 2.7f && mineHelm1 == false && mineHelm2 == false && torchInRange == false){ //to turn cover back on
     cover.GetComponent(SpriteRenderer).enabled = true;
     }
 else if(range >= 5.3f && mineHelm1 == true && torchInRange == false){
     cover.GetComponent(SpriteRenderer).enabled = true;
     }
 else if(range >= 7.9f && mineHelm2 == true && torchInRange == false){
     cover.GetComponent(SpriteRenderer).enabled = true;
     }
 for(var torch : GameObject in torches){ //this is part where it looks for torches and turns off cover sprites
     var rangeTorches = Vector3.Distance(this.transform.position,torch.transform.position);
     //^^ distance between block and torch
     if (rangeTorches <= 7.9f){ //to turn cover off
         cover.GetComponent(SpriteRenderer).enabled = false;
         torchInRange = true;
         }
     else if (rangeTorches >= 7.9f){ //to turn back on
         cover.GetComponent(SpriteRenderer).enabled = true; 
         torchInRange = false;
         }
     }
 }



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 Immanuel-Scholz · Nov 14, 2014 at 07:44 AM 0
Share

I can't see an obvious mistake in your code. $$anonymous$$aybe the torches are not correctly tagged with "torch"? But another hint once you got this working: Using any GameObject.Find* function in an Update() of many game objects (like in "every coverblock") is going to kill your performance VERY quickly. You should use static Lists where the torches/player are add/remove themself on OnEnable/OnDisable. Edit: Scratch this. GameVortex had the superiour answer. ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by GameVortex · Nov 14, 2014 at 07:49 AM

After you have found a torch in range you still continue the loop to check for more torches. This means that it is the last torch in the loop that will be used, and it is highly unlikely that torch is the one that is closest. You need to exit the loop as soon as you find a torch in range. You can do that with the break command:

  if (rangeTorches <= 7.9f){ //to turn cover off
     cover.GetComponent(SpriteRenderer).enabled = false;
     torchInRange = true;
     break; //break here to exit the loop because we found a torch nearby
 }

By the way, you are using both FindGameObjectWithTag and Find functions at the start of the Update. These are slow functions not recommended to be used in each Update. You also probably have a lot of these mining objects in your game, and they Find functions will most likely cause lag problems eventually. I recommend doing the check in a Torch script instead when it is placed and then storing the result. Likewise you can do the player range check on the Player instead, and apply the range to the nearby mining objects.

Comment
Add comment · Show 1 · 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 Blobfysh · Nov 14, 2014 at 09:20 PM 0
Share

Thank you!!! :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Simple for loop to change QualityLevel 1 Answer

How to play an animation at least once before changing 2 Answers

Get the number of items for loop returns 1 Answer

Loop not executing all code - for in loop 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