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 28, 2013 at 01:44 PM · gameobjectarrayfindgameobjectswithtag

HELP Find gameObject With tag in another array

those are my vars...

   var InBag:GameObject[];
   var InBag2:GameObject[];

so i want to find every game object with a certain tag inside the

  var InBag:GameObject[];

and add it to the

 var InBag2:GameObject[];

here im with my script

 var InBag:GameObject[];
 var InBag2:GameObject[];
 
 function Refresh()
 {
 for(InBag2: GameObject in InBag)
 InBag2= GameObject.FindGameObjectsWithTag("Object");
 }
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

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

Answer by GuyTidhar · Mar 28, 2013 at 01:55 PM

'FindGameObjectsWithTag' searches through all the active game objects in the scene, not through a specified list.

Therefor you need a dynamic list in memory (dynamic in the sense that you dynamically and automatically in Unity's case assign more memory for your array items as you go along adding more to your list).

 import System.Collections.Generic; // Don't forget to place this at top of file!

 var InBag:GameObject[];
 var InBag2:GameObject[];

 // Update the refresh function as follows
 function Refresh()
 {
     // Prepare a temporary place in memory to add GameObjects to
     var temp : List.<GameObject> = new List.<GameObject>();
     // Go through all GameObjects in InBag
     for(var inBag : GameObject in InBag)
     {
             // Does the tag of the current GameObject 'inBag' equal "Object"?
         if ( inBag.CompareTag("Object") )
             temp.Add(inBag); // It does - so add it to the temporary list
     }

     // Now take all existing GameObjects from the temporary list and export it to a regular built-in array called 'InBag2'
     InBag2 = temp.ToArray();
 }
Comment
Add comment · Show 10 · 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 GuyTidhar · Mar 28, 2013 at 03:15 PM 0
Share

Let me know if this does the trick.

avatar image hotozi · Mar 28, 2013 at 03:49 PM 0
Share

actually it work....but the things is that...this is for another trick too...because using list system...doesnt match with my type of inventory system...so because i cant remove stuff from my inventory without using list syteme...i tried to refresh my InBag...so i replace the deleted object by a untagged object....and i want to add all the gameobject with OBJECT tag on it to an other than reset my INBag and add InBag2 to InBag...here is my real script...i didnt want it to be so complicated but doesnt work..

 //////////Object bag
 var TargetObject:Transform;
 //////////bag settings
 var Capacity:int=100;
 var InBag:GameObject[];
 var InBag2:GameObject[];
 
 function Ad(){InBag+=[TargetObject.gameObject];}
 
 function Refresh()
 {
 for(InBag2: GameObject in InBag){  'eror line'
 InBag2= GameObject.FindGameObjectsWithTag("Object");
 }
 InBag=new GameObject[0];
 for(var i = 0; i < InBag2.length; i++){InBag+=[InBag2[i]];}
 }
avatar image GuyTidhar · Mar 28, 2013 at 03:57 PM 0
Share

1.

This line:

 GameObject.FindGameObjectsWithTag("Object");

Searched all the game object of your scene, and not only within InBag. Judging from what you wrote that is not what you need.

I'd advise you to keep using my suggestion.

2.

What else do you wish to do with your inventory system?

You can still use List, but I'll need to know what you wish to do with it.

$$anonymous$$y suggestion only uses the List collection in order to temporary hold your required items.

Do not remove:

 var InBag:GameObject[];
 var InBag2:GameObject[];

(I have now edited my answer to show these two lines).

3.

 'for(InBag2: GameObject in InBag)'

Is not the correct syntax.

You should be doing:

 for(var singleItemFromInBagInventroy : GameObject in InBag)
 {
    // singleItemFromInBagInventroy  is a game object within InBag
 }
avatar image hotozi · Mar 28, 2013 at 04:21 PM 0
Share

Soppose i use ur first script...how do i put back the stuff inside InBag? cuz its not a ''real array'' compare to the InBag2...?

avatar image GuyTidhar · Mar 28, 2013 at 10:18 PM 0
Share

When are you removing anything from InBag?

InBag and InBag2 are both built-in GameObject arrays.

What do you need to "put back" into InBag?

Show more comments

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Array weapons Wheel scroll switch 1 Answer

Array problem? help please! 1 Answer

Cant change GO material with multipli material 1 Answer

Array Help GameObject Length Inventory 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