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
1
Question by Wikened · Jan 15, 2012 at 12:01 AM · arraygameobjectsfindwaypointdelete

C# Array problems

I create an array of GameObjects called WayPoints and then find the closest one, but then I can't seem to understand how to delete the waypoint from the Array once my cube reaches it?

HELP! :/


public GameObject[] WayPoints;

 GameObject FindWaypoint()
     {
         WayPoints = GameObject.FindGameObjectsWithTag("Waypoint");
         GameObject closest = null;
         float distance = Mathf.Infinity;
         Vector3 position = transform.position;
         
         foreach (GameObject item in WayPoints) 
         {
             Vector3 diff = item.transform.position - position;
             float curDistance = diff.sqrMagnitude;
             
             if (curDistance < distance) 
             {
                 closest = item;
                 distance = curDistance;
             }
         }
         return closest;
     }
 
 void Awake () 
 {
 WayPoints = GameObject.FindGameObjectsWithTag("Waypoint");
 }
 
 void Update()
 {
 if(Vector3.Distance(gameObject.transform.position,TargetTile.transform.position) > 0.1)
         {
             transform.position = Vector3.Lerp(transform.position, TargetTile.transform.position, (Time.time - startTime) / duration);
         }
         else {
             TargetTile = FindWaypoint();
         }
 }
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
2

Answer by Robinmtb · Jan 15, 2012 at 12:35 AM

Hi!

This should do the trick :)

 int WaypointIndex = Array.IndexOf(Waypoints, TargetTile); //Find index of reached waypoint
 List<GameObject> temp = new List<GameObject>(Waypoints); //Duplicate old Array into a List
 temp.RemoveAt(WaypointIndex); //Remove reached waypoint from templist
 Waypoints = temp.ToArray(); //Update array
Comment
Add comment · Show 2 · 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 Wikened · Jan 15, 2012 at 12:52 AM 0
Share

Where do I place everything? I keep getting errors:

"CS0246: The type or namespace name List1' could not be found. Are you missing a using directive or an assembly reference?"

avatar image Bunny83 · Jan 15, 2012 at 02:48 AM 0
Share

You need the System.Collections.Generic namespace where the generic List<> is defined in by adding

 using System.Collections.Generic;

at the top of your script.

Alternatively you can use the full classname:

 System.Collections.Generic.List<GameObject> temp = new System.Collections.Generic.List<GameObject>(Waypoints); 

which is not really a good thing ;)

avatar image
1

Answer by aldonaletto · Jan 15, 2012 at 04:50 AM

WayPoints is a builtin array, and you can't remove elements from it easily - you must create a temp array with one less element, copy to it all waypoints but the one you want to delete and assign the temp array to WayPoints.
I made a few changes in your script to include this delete function, and reload the waypoints when needed. I also changed Lerp to MoveTowards and defined a speed variable instead of duration: this is a better alternative when you want to move at constant speed.

public float duration = 10; public float speed = 2.5f; public GameObject[] WayPoints; private GameObject TargetTile;

void DeleteWaypoint(GameObject waypoint) { GameObject[] temp = new GameObject[WayPoints.Length-1]; int i = 0; foreach (GameObject elem in WayPoints){ if (elem != waypoint) temp[i++] = elem; } WayPoints = temp; }

GameObject FindWaypoint() { if (WayPoints.Length == 0){ // if no waypoints, find them WayPoints = GameObject.FindGameObjectsWithTag("Waypoint"); } GameObject closest = null; float distance = Mathf.Infinity; Vector3 position = transform.position; foreach (GameObject item in WayPoints) { Vector3 diff = item.transform.position - position; float curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = item; distance = curDistance; } } DeleteWaypoint(closest); // delete this waypoint from the list return closest; }

void Update() { if (TargetTile && Vector3.Distance(transform.position, TargetTile.transform.position) > 0.1) { transform.position = Vector3.MoveTowards(transform.position, TargetTile.transform.position, Time.deltaTime * speed); } else { TargetTile = FindWaypoint(); // get next waypoint; } }

Comment
Add comment · Show 2 · 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 Wikened · Jan 15, 2012 at 08:26 AM 0
Share

Both of them worked great! Thanks aldonaletto for writing it out!

avatar image Meltdown · Jan 15, 2012 at 08:31 AM 0
Share

Don't post comments as answers. Rather comment on any answers that helped you. Please also 'mark as answer' the post that helped you most so we know your question is resolved.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gather all GameObjects with a certain name in a Scene and Apply them to a list? 3 Answers

Moving a GameObject (camera) forward and backward along a series of points (C#). 1 Answer

How can I detecting whether certain characters appear in any strings in my array of strings? 1 Answer

How do I make an array and then access an Object from the array 2 Answers

Question on for loop with array 0 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