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 azmundai · Apr 29, 2015 at 02:12 AM · gameobjectprefablistremovecompare

Find specific prefab in a list

Hi, Essentially I need to remove a specific game object from a list, but I am not initially using the game object from the list .. so how do I figure out which number in the list refers to the game object I am using?

The function basically needs to do this :

 public void RemoveObject(GameObject go){
     for(int i = 0; i < objects.Count - 1; i++){
         if(go == objects[i]){
             objects.RemoveAt(i);
         }
     }        
 }
Comment
Add comment · Show 1
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 Bunny83 · Apr 29, 2015 at 10:07 PM 0
Share

btw: Your for loop will skip the last item since the highest possible index is (Count-1) but your condition is that i has to be smaller than (Count-1) so your last used index will be (Count-2).

It should look like this:

 for(int i = 0; i < objects.Count; i++){

2 Replies

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

Answer by Kiwasi · Apr 29, 2015 at 02:23 AM

List.Remove?

Comment
Add comment · Show 3 · 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 azmundai · Apr 29, 2015 at 02:35 AM 0
Share

thanks, but are you sure that works? Im probably reading too much into this, but it says "Removes the first occurrence of a specific object from the ICollection." Since the list is a collection of prefabs of the same object, is it really removing the specific object, or the first occurrence of the prefab?

Sorry if I am overthinking things ..

avatar image brunopava · Apr 29, 2015 at 12:25 PM 0
Share

It removes based on the instance, not on the type. So if you pass the instance of the object you want to remove, it will sure remove the right one.

So ins$$anonymous$$d of using the for, you can just use:

 objects.Remove(go);
avatar image Bunny83 · Apr 29, 2015 at 01:32 PM 0
Share

If his original code doesn't work, Remove won't work either because it does exactly the same ^^.

avatar image
0

Answer by Bunny83 · Apr 29, 2015 at 01:43 PM

Your question is a bit vague. You talk about prefabs and gameobjects and it's not clear what variables hold what.

I guess that your "objects" list contains prefab references. So references to assets in your project. What exactly is "go" referencing? If it's an instance of a prefab, there's no way your code will work as when you instantiate a prefab, you just create a clone of it. The clone has no relation to the prefab at all. It's just a standalone GameObject from now on.

If you want to remember from which prefab an object was cloned, you have to manually store that information inside a script on that object. Keep in mind that a prefab can't hold a reference to itself. Because when you instantiate the prefab, all selfreferences are replaced with the instance. What you can do is re-assign the reference after the instantiate line:

 public class ScriptOnPrefab : MonoBehaviour
 {
     public GameObject prefabReference;
 }

When you instantiate your prefab do it like this:

 GameObject clone = (GameObject)Instantiate(prefab);
 clone.GetComponent<ScriptOnPrefab>().prefabReference = prefab;

That way you can always use that reference on the instance to identify the prefab from which it has been cloned.

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 azmundai · May 06, 2015 at 11:30 PM 0
Share

Thanks for the comment, I guess I have to do more testing, especially with your comment on my original post, but boredmoron's approach seems to work.

To clarify, I have created 2 lists at start(). 1 for active objects and 1 for inactive objects. They are all clones from the same prefab.

When 1 is destroyed, in becomes inactive, later it might become active again. $$anonymous$$aking it active again is easy. What I was struggling with is how to remove the item I just "hit" from the active objects list.

This is what I am doing currently :

 public void ResetObject(GameObject go){
     go.transform.FindChild("$$anonymous$$yObject").GetComponent<Object$$anonymous$$ovement>().ResetPosition();
     inactiveObjects.Add(go);
     activeObjects.Remove(go);
 }
avatar image Bunny83 · May 07, 2015 at 02:35 AM 0
Share

@azmundai: In that case the term prefab is wrong here ^^ If you instantiate a prefab it's just a gameobject.

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

21 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 avatar image avatar image

Related Questions

In a tile-based game, best way to select multiple tiles via dragging? 1 Answer

Remove all duplicates from a list 1 Answer

How to remove objects from a list ? 3 Answers

moving starfield with gameobject prefab list 1 Answer

compare raycast hit with list of gameobjects c# 2 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