Search Unity

ADD ONE MORE VARIABLE IN THE GAMEOBJECT CLASS

Discussion in 'Scripting' started by kevinzou, Nov 21, 2014.

  1. kevinzou

    kevinzou

    Joined:
    Nov 21, 2014
    Posts:
    5
    pls consider to add one more variable in the gameobject class, which is the Object Class type. This variable is for the developer to tag any obj they want. For example, now we write one component and attached to this gameobject. After that, we want to find this component, we have to use the getcomponent or something. But if we can tag our component to this variable, we can access our component directly once we have the gameobject. No need to waste the time to getcomponent.
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Firstly, seriously, is your caps lock broken? Might want to get it checked out.

    About your idea, this will cause a lot of problems. What happens if some code assumes that this "object" property on a game object is of class a but actually it is class b? The moment you try and cast it you are going to get exceptions popping up. This defeats OOP. Downcasting is usually best avoided when possible as it stops the compiler from catching parameter miss matches.

    Instead of a lot of GetComponent calls, you are now going to have a lot of casting tests. Replacing one problem with another. If you need a lot of interaction between components, consider having properties you can set in the editor or using events and delegates to pass around messages. Also caching GetComponent (so you arent doing it every frame) works well.
     
  3. kevinzou

    kevinzou

    Joined:
    Nov 21, 2014
    Posts:
    5
    1, This should be maintenance by the developer, and this case still will happen even you don't take my idea.
    2, And the edit can catch the exception if in this case, that means we can get the notice and revise the "object" as class b, not class a. Will not cause the program clash in further.
    3, I just take GetComponent for example, some times I attach to the GameObject is not the obj base from MonoBehaviour, it's other obj. So any componets method can not be use. Yes, you are right, we have many way to access this obj. But the most efficiency way is directly access it, do you think so?
    4, before I develop in D3D11+PhysX, with C++, C++/CLI and C#. Many PhysX offical example use this idea. If you have time, pls study their example.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    And hows your soluton handling multiple instances of a component on a single object

    Just cache your references, much simple
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    If you're wanting something similar to how you can do gameObject.collider or gameObject.rigidbody, you'll want to know that even these are being removed in Unity 5 in favor of GetComponent as they aren't guaranteed to exist.
     
  6. kevinzou

    kevinzou

    Joined:
    Nov 21, 2014
    Posts:
    5
    Sorry, I don't understand this fully, can you explain what will happen if "multiple instances of a component on a single object"
    Yes, now I am taking the way " cache the references" outside the GameObject. But sometimes it will need more coding, and more skill to get my purpose in the actual project. So I suggest this to make the things more easy, but if they don't like, ok, just let it be.
     
    Last edited: Nov 22, 2014
  7. kevinzou

    kevinzou

    Joined:
    Nov 21, 2014
    Posts:
    5
    oh, it's too bad. but they have a more efficiency to do the same. Just do as their idea.
    But the way, if there is some way to inherit the GameObject class, for example,
    Class GameObjectEx:GameObject{}
    and how to use this new class?
     
    Last edited: Nov 22, 2014