Search Unity

  1. We want you to join us at GDC this year! We've added new sessions to Unity Central, space is limited so sign up now!
    Dismiss Notice
  2. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  6. Be the first to take a peek at upcoming 2D Animation Preview. Drop into the forum and check it out!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    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.