Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  6. Unity 2017.2 beta is now available for download.
    Dismiss Notice

Keep components as fields inside a script?

Discussion in 'Scripting' started by mahdiii, Sep 14, 2017.

  1. mahdiii


    Oct 30, 2014
    Hi all. Suppose we have two scripts like ScriptA and ScriptB for obj1.
    Then ScriptC for obj2 has an array of obj1 and needs to access to ScriptA and ScriptB objs1.
    We can write obj1.GetComponent<ScriptA>() and obj1.GetComponent<ScriptB>()
    or can write ScriptA scriptA=obj1.GetComponent<ScriptA>() and scriptA.scriptB, if we have
    Code (CSharp):
    1. public class ScriptA:Monobehaviour {
    2. ScriptB scriptB;
    3. }
    but it creates extra depandancy and coherence. How can we reduce these coherences?
    Do we keep the references of components only when we need them inside the script?
  2. KelsoMRK


    Jul 18, 2010
    Short answer is: yes - don't junk up your behaviours with references they never use (the compiler even warns you about this).
    mahdiii likes this.