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 FirePlantGames · Feb 23, 2014 at 05:16 PM · arrayremove

array question

so i need to remove an element of an array NO LIST!!!

so, i tried use removeAt but it gives me an error and doesn't remove the element: missing method expectation

here's the code:

 var waypoints : GameObject[];
 var closestWaypoint : GameObject;
 @HideInInspector
 var distance = Mathf.Infinity;
 var moveTowardWaypoints : boolean;
 var speed : float;
 var maxSpeed : float = 2;
 
 var horizontalMovement : Vector2;
 var lastWaypoint : GameObject;
 var lookForNewWaypoint : boolean;
 
 function Update()
 {
     transform.LookAt(closestWaypoint.transform);
     transform.rotation.x = 0;
     transform.rotation.z = 0;
     
     horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
     if (horizontalMovement.magnitude > maxSpeed)
     {
         horizontalMovement = horizontalMovement.normalized;
         horizontalMovement *= maxSpeed;
     }
     
     rigidbody.velocity.x = horizontalMovement.x;
     rigidbody.velocity.z = horizontalMovement.y;
     
     waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
 }
 
 
 function LateUpdate()
 {
     if (lookForNewWaypoint)
     {
         for (var go : GameObject in waypoints)
         {
             var diff = (go.transform.position - transform.position);
             var curDistance = diff.sqrMagnitude;
             if (curDistance < distance)
             {
                 closestWaypoint = go;
                 distance = curDistance;
                 lookForNewWaypoint = false;
             }
         }
     }
     
     
     if (lastWaypoint)
     for(var i : int = 0; i < waypoints.Length; i++)
     {
         if(waypoints[i].gameObject == lastWaypoint)
         {
             waypoints.RemoveAt(i);
         }    
     }
 }
 
 
 function FixedUpdate()
 {
     rigidbody.AddRelativeForce(0,0,1 * speed);
 }
 
 
 function OnTriggerEnter(col : Collider)
 {
     if (col.transform == closestWaypoint.transform)
     {
         if (!lastWaypoint)
         {
             lastWaypoint = col.gameObject;
             lookForNewWaypoint = true;
         }
     }
 }
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
1
Best Answer

Answer by robertbu · Feb 23, 2014 at 05:46 PM

You cannot removed items from a built-in array. So your choices are:

  • Convert your code to use something like a List that has a RemoveAt() and use that in your code.

  • Convert back and forth between a built-in array and some array that has RemoveAt (link below).

  • Write your own RemoveAt(). You could copy all the elements above the removal point down one and then use System.Array.Resize() to change the size of the array.

http://answers.unity3d.com/questions/17033/how-do-you-use-removeat-or-add-with-an-array-of-ga.html

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 FirePlantGames · Mar 30, 2014 at 03:49 PM 0
Share

i felt sooooo stupid reading this!(i actually new this from another project) thank you so much for the help!

avatar image
0

Answer by YoungDeveloper · Feb 23, 2014 at 05:56 PM

@robertbu pretty much explained it all. But consider that array copying or "resizing", which is copying too, is an expensive process, especially if arrays are large. So i suggestion is if you have array of max waypoints, lets say 50, and any action in the future will be between 0 and 49 waypoints, there's really no need to resize the array after each removed of added element just to save that tiny tiny amount of ram, you will be doing a ton more calculations. You can always set gameobject element to null, and consider it as empty.

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

How to remove an object from an array once it has been destroyed 1 Answer

identify and remove an item from a list 1 Answer

problem adding element of array 1 Answer

Remove object from Array in javascript 1 Answer

Remove element from array 3 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