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 seashell · Feb 13, 2014 at 01:21 PM · findclass objectclearclass instance

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]?

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
0

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.

Comment
Add comment · Show 1 · 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 seashell · Feb 13, 2014 at 02:33 PM 0
Share

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?

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

19 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

Related Questions

How do I check if a derived class is a certain class? 1 Answer

How do I check if a derived class is a certain class? 0 Answers

[CLOSED] How do I check if a derived class is a certain class? 0 Answers

How to create a repository of enemy objects that are initialised but then get a new instance of a random one? 1 Answer

Every active Object a Script? no some efficient way? 4 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