• 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 awplays49 · Mar 29, 2015 at 03:26 PM · c#loops

Piece of code including foreach loop not working correctly

Everytime I press play, "Distance" is always 0… What am I doing wrong?

 if (DataValue == CubeManagerGet.GetComponent <CubeManager> ().CurrentCube)
         {
             TextGet.text = "Coordinates: {" + transform.position.x + ", " + transform.position.y + "}";
             Debug.Log ("YES");
             GameObject [] Cubes = GameObject.FindGameObjectsWithTag ("Cube");
             float Distance = Mathf.Infinity;
             foreach (GameObject CubeTag in Cubes)
             {
                 if (Vector3.Distance (transform.position, CubeTag.transform.position) < Distance)
                 {
                     Distance = Vector3.Distance (transform.position, CubeTag.transform.position);
                 }
                 if (Distance <= 5)
                 {
                     CanSubtract = true;
                 }
                 else
                 {
                     CanSubtract = false;
                 }
             }
             Debug.Log (Distance);
         }
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

2 Replies

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

Answer by sumeeton · Mar 29, 2015 at 03:45 PM

Why this statement?

 if (Vector3.Distance (transform.position, CubeTag.transform.position) < Distance)

Is any of your cube at position of the object the script is attached to? Remove the above check.

Edit your loop as following:

 foreach (GameObject CubeTag in Cubes)
 {
       Distance = Vector3.Distance (transform.position, CubeTag.transform.position);
     if (Distance <= 5)
     {
          CanSubtract = true;
         }
     else
     {
          CanSubtract = false;
     }
      Debug.Log(Distance);
 }
Comment
Add comment · Show 28 · 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 awplays49 · Mar 29, 2015 at 03:48 PM 0
Share

So that it sets distance to the smallest distance. However, I am trying to make an alternative solution that doesnt mean checking if there is something there.

This code is supposed to delete the current cube, then check every slot and the lowest one that exists if the one the camera is teleported to. However, its not working.

 if (Action == Actions.Subtract && Cube$$anonymous$$anagerGet.GetComponent <Cube$$anonymous$$anager> ().CubeCount > 1)
         {
             Destroy (Cube$$anonymous$$anagerGet.GetComponent <Cube$$anonymous$$anager> ().Cubes [Cube$$anonymous$$anagerGet.GetComponent <Cube$$anonymous$$anager> ().CurrentCube]);
             TextGet.text = "No Cube Selected";
             TextGet.color = Color.red;
             for (int CubeReplace = 0; CubeReplace < Cube$$anonymous$$anagerGet.GetComponent <Cube$$anonymous$$anager> ().Cubes.Count; CubeReplace ++)
             {
                 if (Cube$$anonymous$$anagerGet.GetComponent <Cube$$anonymous$$anager> ().Cubes [CubeReplace] != null)
                 {
                     Cube$$anonymous$$anagerGet.GetComponent <Cube$$anonymous$$anager> ().CurrentCube = CubeReplace;
                     Camera.main.transform.position = new Vector3 (Cube$$anonymous$$anagerGet.GetComponent <Cube$$anonymous$$anager> ().Cubes [Cube$$anonymous$$anagerGet.GetComponent <Cube$$anonymous$$anager> ().CurrentCube].transform.position.x, Cube$$anonymous$$anagerGet.GetComponent <Cube$$anonymous$$anager> ().Cubes [Cube$$anonymous$$anagerGet.GetComponent <Cube$$anonymous$$anager> ().CurrentCube].transform.position.y, 0);
                     break;
                 }
             }
         }
avatar image sumeeton · Mar 29, 2015 at 03:52 PM 0
Share

The distance is not reset in the loop. How can you be sure that the cubes in the loop are in the descending order of the distance. Your current check will ignore most of the cubes.

avatar image sumeeton · Mar 29, 2015 at 03:58 PM 0
Share

I hope you are getting it. What if the first calculated distance is 0? Try removing the check.

avatar image awplays49 · Mar 29, 2015 at 04:09 PM 0
Share

I already got rid of that code.. It would have been not very efficient apparently so i have heard. Im trying to make a game with an ortho camera looking at cubes and you can remove and add cubes. when you remove, however, i want it to delete yourself, and then you just click on another block to inhabit that block. it could possibly leave you on a screen, though, where there is no blocks to click, and thats what im having trouble with.

avatar image sumeeton · Mar 29, 2015 at 04:20 PM 0
Share

Okay! What about the distance now? Does the debug shows different distances? Your code isn't neat. Sorry for that. I am unable to understand the algorithm you are implementing.

Show more comments
avatar image
0

Answer by _joe_ · Mar 29, 2015 at 03:48 PM

I went through your code and fixed a couple of things, I removed something that you need to put back (only because i don't have the whole code, so for instance the first if statement, and the CanSubtract which is now a simple Debug (just put back your own code).

Although i don't really understand what you're trying to do, here's the code that worked and let me know if you need anything else, Distance is successfully changing and the if tests are working. Thanks for voting up and accepting as "Answered".

 using UnityEngine;
 using System.Collections;
 
 public class HasMoved : MonoBehaviour {
 
     public GameObject [] Cubes;
     float Distance;
 
     void Start(){
         Debug.Log( "Coordinates: {" + transform.position.x + ", " + transform.position.y + "}");
         Cubes = GameObject.FindGameObjectsWithTag ("Cube");
 
         Distance = Mathf.Infinity;
     }
 
     void Update(){
         foreach (GameObject CubeTag in Cubes)
         {
             if (Vector3.Distance (transform.position, CubeTag.transform.position) < Distance)
             {
                 Distance = Vector3.Distance (transform.position, CubeTag.transform.position);
             }
             if (Distance <= 5)
             {
                 Debug.Log("Can Subtract True");
             }
             else
             {
                 Debug.Log("Can Subtract False");
             }
         }
         Debug.Log (Distance);
     }
 
 }
 
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

Unity 3.5 C# Problem 1 Answer

An OS design issue: File types associated with their appropriate programs 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