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 hotozi · Mar 27, 2013 at 02:04 AM · gameobjectarrayinventory

HELP How to remove gameobject from Built-in Arrays

basicaly i just want to know how do u remove a gameobject from a Built-in Arrays if u dont know...its that

var BLA:GameObject[];

i know ther are a lot of arrays that can be better but i tried and that one is better for what i want to do...so if u know...if u have reference....tell me PLZ!

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 whydoidoit · Mar 27, 2013 at 02:07 AM

You can't do that easily. You need to use a List.< GameObject> if it needs to be added to or removed from while the game is playing.

Comment
Add comment · Show 7 · 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 whydoidoit · Mar 27, 2013 at 02:10 AM 0
Share

You could do either of these:

  var newArray = new GameObject[BLA.Length-1];
  for(var i = 0; i < BLA.Length-1; i++)
        newArray[i] = BLA[i];
  BLA = newArray;

This would remove the last item, you could change it to remove any old item with a bit of work. Nasty though - use a List!

Also you could use Linq

  Import System.Linq;
  ...
  var thingToRemove = someGameObject;
  BLA = BLA.Where(function(g) g != thingToRemove).ToArray();

Again - you'd be better off using a List

  List.Remove(someGameObject);
avatar image hotozi · Mar 27, 2013 at 02:38 AM 0
Share

i do understand but im lost...this is my script...how do u gonna do the first one in it....

this is on a GUI function.....how do u expect it....

 > for(var x=0;x <Inbag.Length;x++)
 {
 var ITE$$anonymous$$= InBag[x];
 GUILayout.BeginHorizontal();
 GUILayout.Button(ITE$$anonymous$$.GetComponent(InItems).Icon);
 GUILayout.BeginVertical();
 GUILayout.Box(ITE$$anonymous$$.name);
 GUILayout.BeginHorizontal();
 if(GUILayout.Button("Use"))
 {
  " here when i click here i want to remove it from the thing"
 }
 GUILayout.EndHorizontal();
 GUILayout.EndVertical();
 GUILayout.EndHorizontal();
 GUILayout.EndArea();
avatar image hotozi · Mar 27, 2013 at 02:41 AM 0
Share

CUZ I HAVE THE PERFECT INVENTORY SYSTE$$anonymous$$$$anonymous$$.....BUT THE THING IS THAT i cant remove thing...i realy need this

avatar image whydoidoit · Mar 27, 2013 at 02:41 AM 0
Share
  var newArray = new GameObject[InBag.Length-1];

  var j = 0;
  for(var i = 0; i < InBag.Length; i++)
  {
        if(InBag[i] != ITE$$anonymous$$) 
          newArray[j++] = InBag[i];
  }
  InBag = newArray;
avatar image whydoidoit · Mar 27, 2013 at 02:43 AM 0
Share

It's late - I must have typed that script 6 times :D Should be right now...

Show more comments
avatar image
0

Answer by Bluestrike · Mar 27, 2013 at 08:26 AM

 //remove item in slot 0
 InBag.RemoveAt(0); 


Everything slides up so what was in slot 1 now became slot 0.

Array functions list with examples at the bottom:

http://docs.unity3d.com/Documentation/ScriptReference/Array.html

Comment
Add comment · Show 5 · 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 Bluestrike · Mar 27, 2013 at 08:40 AM 0
Share

Also if you have a inventory system you may want to use a empty object as placeholder ins$$anonymous$$d of removing items. This way everything stays in place.

ins$$anonymous$$d of removing an item you replace it with an emtpy placeholder.

 var empty :gameObject; // assign a "empty" prefab item.
 
 //Clearing a slot:
 InBag[5]= empty;

When checking if a slot is availeble you just need to check if it hold the empty prefab.

 if(InBag[5] == empty)
 { 
  // Slot is availeble.
 }
avatar image hotozi · Mar 27, 2013 at 11:58 AM 0
Share

yeah!!! right i'll keep that in $$anonymous$$d in case...but the things is that the gameobject will be in the length and i want to remove it....exept if i can findgameObject by component...it will be great?

avatar image hotozi · Mar 27, 2013 at 12:09 PM 0
Share

I know its an other question but i have an idea... supose i have this

 var BLA:GameObject[];
 var BLA2:GameObject[];

i put an empty gameobject to replace the one i dont want...or delete......then by finding every gameobject with a script attach to it.....i put those inside the var BLA2 after resize it to 0 then i resize the var Bla to 0...so the empty gameobject will be delete...then i put back the objects inside the BLA2 in the var BLA...?

avatar image Bluestrike · Mar 27, 2013 at 01:48 PM 0
Share

Not sure what you mean but you can use a for loop to iterate trough every object in the array and check that object for a sertain component or script value as shown in whydoidoit awnsers.

avatar image frogsbo · Jun 07, 2014 at 06:36 PM 0
Share

I seem to remember vaguely that there is an obscure built in array command that does RemoveAt, that eric5h5 said somewhere, but i forgot it. it's a strange syntax of RemoveAt i dunno. i need it now i can't remember it!

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Array Help GameObject Length Inventory 1 Answer

Array problem? help please! 1 Answer

Array weapons Wheel scroll switch 1 Answer

HELP Find gameObject With tag in another array 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