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 /
This question was closed Jul 30, 2014 at 08:04 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Vertedex · Jul 30, 2014 at 07:55 AM · c#arrayforeach

[SOLVED] Problem with "foreach".

I'm making script that'll select nearest object with tag. It's this:

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class IfCloseEnought : MonoBehaviour {
 
     public GameObject[] enemies;
     public float range = 10;
     public float minimalDistance = 65879;
     public float measureDistance;
     public GameObject target;
     private int number;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         enemies = GameObject.FindGameObjectsWithTag ("Enemy");
         foreach (GameObject enemy in enemies) {
             measureDistance = Vector3.Distance(gameObject.transform.position, enemy.transform.position);
             if(measureDistance < minimalDistance && measureDistance < range){
                 target = enemy;
                 minimalDistance = measureDistance;
                 number++;
             }
         if(target == null){
                 minimalDistance = 65879;
         }
         if(target != null){
             if(Vector3.Distance(gameObject.transform.position, target.transform.position) > range){
                     target = null;
             }
         }
             Array.Clear(enemies, 0, enemies.Length);
             number = 0;
             Debug.Log(target);
             Debug.Log(minimalDistance);
         }
 
 
     }
 }
 

The problem is somewhere in foreach loop (i think). It's executed only on 0th element of array, and doesn't calculate distance between gameObject and elements 1, 2, 3...

It also outputs this error:

NullReferenceException: Object reference not set to an instance of an object IfCloseEnought.Update () (at Assets/Scripts/IfCloseEnought.cs:22)

Where's problem and how to fix it?

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

  • Sort: 
avatar image
0
Best Answer

Answer by superpig · Jul 30, 2014 at 07:58 AM

Looks like you've got mismatched braces. The foreach loop begins at line 21, but ends at line 40; I think you want it to end between lines 27 and 28 instead.

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 Vertedex · Jul 30, 2014 at 08:01 AM 0
Share

Thank you for such quick help! Now it works just fine! Thanks!

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Array foreach problem 1 Answer

I have to set a Collider[] to Collider - what do? 1 Answer

Problem with World Generate 1 Answer

difference between GameObject.Find() and FindGameObjectWithTag 1 Answer

C# For loop in button to set gameObjects in array to active 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