- Home /
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);
}
}
}
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++){
Answer by Kiwasi · Apr 29, 2015 at 02:23 AM
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 ..
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);
If his original code doesn't work, Remove won't work either because it does exactly the same ^^.
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.
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);
}
@azmundai: In that case the term prefab is wrong here ^^ If you instantiate a prefab it's just a gameobject.