- Home /
C# finding gameobject that created this class, and do I need to clear them?
Hello! I'm learning now OOP. And I got some questions, about how unity3d works with classes.
First situation: I'm having a GameObject that has a script:
Bot Script:
public class Bot : MonoBehaviour
{
void Start ()
{
....EnemysList.Add(new Enemy(this.gameObject, 100));
}
}
====================
Where Enemy class is this:
public class Enemy : Enemys
{
public int EnemysCount;
private int Health;
private GameObject Owner;
public Enemy(GameObject WhoCreated,int newHealth)
{
EnemysCount++;
Owner = WhoCreated;
Health = Health;
}
public int Master
{
get
{
//Returning class owner
return Owner;
}
}
}
And Enemy is a part of Enemys class, that stores in a list all new Enemy classes ( EnemysList):
So First question is: If I want to know who is owner of class from EnemysList(that stores all classes that were created by Bot on Start()), is there better way to access GameObject, than Enemys.EnemysList[0].Master? I mean, that should I really add to class GameObject variable? Or there is another way of doing it?
Second question is: If my GameObject with Bot script, will be removed, do I need to remove from list that class? Or Unity3d does it by itself? And if I need, how can I remove class from list Enemys.EnemysList[n]?
Answer by nesis · Feb 13, 2014 at 02:00 PM
First question:
I'm a little unclear about what kind of design pattern you're trying to implement there... It sounds like you're wanting to create a factory you can create multiple instances of, with each instance maintaining a list of objects it creates? If that's the case, you'll want to store a reference to an instance of the factory class in each object it creates (so they can reference their "owner").
Second question:
After your enemy instance is destroyed, the reference to it in your list will become null, but there will still be an element taken up by that null reference. You can handle removing your enemy from the list by using OnDestroy() in your enemy class (if it inherits from MonoBehaviour) or your enemy class' destructor (if it doesn't inherit from MonoBehaviour).
On that note: Be careful with using class constructors and destructors with anything that inherits from MonoBehaviour. Because MonoBehaviours have their constructors and destructors called during events in the editor (begin play, end play, add, duplicate, delete), you can get some odd errors. Generally, when inheriting from MonoBehaviour and attempting to manage initialising and cleaning up objects during play, you should be using Awake() (or Start()) instead of constructors, and OnDestroy() instead of destructors.
It can be represented as a factory, but in my case it's just a group of AI, that are called by order (to do action), and when any of them is Destroyed (not accessible), I need to erase from my list that GameObjects class. But before that, I need to get, to my AI, to access some components and then erase all about it in my scene.
Am I doing it wrong? Accessing GameObject with Enemys.EnemysList[0].$$anonymous$$aster.GetComponent.... Or there is a better way to access a GameObject that created this class?
And about removing Class, how I can do that, if its needs to be manually done? Is there a good example for that? Basically I can check if my Enemys.EnemysList[0].$$anonymous$$aster==null, and then remove, but I have no clue what commands I need to use...
If scene gets reloaded (or I going to load a new one), all classes will cleared automatically?