Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Connecting via Unity's Inspector or by code?

Discussion in 'Scripting' started by MitchStan, Jun 7, 2012.

  1. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    567
    Quick question about performance v. neatness:

    Up until now I have always used the Unity's inspector to connect components and prefabs to public variables. All is good but as a project grows in size, there is a lot of dragging and dropping that has taken place. If some connection accidentally becomes unconnected, it may take a few minutes to find the issue and reconnect.

    Just yesterday I thought why not just connect everything in code so I don't have to be worried about it. So I now I have the following for instance:

    Code (csharp):
    1.        
    2. // Instead of connecting via Unity's Inspector, do it in code
    3. var cargo : Cargo = GetComponent.<Cargo>();
    4. var palette : Palette = GetComponent.<Palette>();
    5. var package : Package = GetComponent.<Package>();
    All works fine but here is my question or concern:

    As this scales up and we are dealing with hundreds of GameObjects and Prefabs, is this costly or expensive in terms of performance? As prefabs are being Instantiated and these connections are being made in Awake(), will this cause a frame rate hit or performance drop?

    Is it worth it - should I just go back to manually connecting in the Unity Inspector?

    Opinions please? Thank you.
     
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
  3. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    567
    Yes, that is correct, but that's not what I'm asking about.

    The question is, with hundreds of prefabs being Instantiated and Destroyed during the course of the simulation, am I asking for trouble in terms of performance if too many prefabs are created at the same time? Is it worth the "neatness" to connect components in code or is it advisable to connect everything via Unity's Inspector?
     
  4. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    The cost of running a handful of GetComponent calls per prefab is trivial in comparison to the cost of actually instantiating the prefab - do what's easier from a development standpoint (in this case, GetComponent saves a ton of "inspector setup" time).

    You could, if you really wanted to, come up with an editor script solution that sets all your references up automatically. I don't think the benefit is worth the development time, honestly - but it's certainly possible.

    EDIT:
    I don't mean to imply that you'll never hit issues instantiating prefabs at runtime, only that you'll hit noticeable performance issues from actually instantiating things far, far faster than you'll hit performance issues due to running GetComponent in awake.
     
  5. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    567
    That's good to know - why be concerned with the negligible hit from GetComponent when Instantiate is the real expense. Thanks for that insight.
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    The general rule I follow is to do hookups in code where they shouldn't ever be changed, do them in the Editor where they need to be changed easily, and be consistent throughout a given project for things which are less clear cut.
     
  7. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    'tis a good rule to follow. Recently I've been going the "FingerGestures" approach - which is to do both...

    Code (csharp):
    1.  
    2. public class Example : MonoBehavior
    3. {
    4.     [SerializeField] ComponentType _target;
    5.    
    6.     void Awake () {
    7.         if (_target == null) _target = GetComponent<ComponentType>();
    8.     }
    9. }
    10.  
    That way I don't have to set things up in the inspector if there's no need - but if I want to do any sort of cross-object referencing it's already set up to do so.
     
  8. sp00ks222

    sp00ks222

    Joined:
    May 12, 2011
    Posts:
    37
    The only problem with this would be when you are removing a component, you would have to make sure you check your code and the inspector. Still the best approach though