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

Tutorial: What was the point in adding scripts as components?

Discussion in 'Editor & General Support' started by Nanako, Sep 30, 2014.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Hi all. I'm on tutorial 106, here: http://unity3d.com/learn/tutorials/projects/stealth/game-controller

    Up until now, i've bneen pleasantly surprised with the simplicity of the component panel, and i was figuring that adding a script onto an object as a component might save me some work/code down the line.

    However, this script is teaching me to do something which i'm quite familiar with, did lots of in as3, and which i believe is redundant, instantiating scripts as variables, and then assigning object references to them

    This example:
    Code (CSharp):
    1.     private AlarmLight alarm;                                           // Reference to the AlarmLight script.
    2.     private Light mainLight;                                            // Reference to the main light.
    3.     private AudioSource panicAudio;                                     // Reference to the AudioSource of the panic msuic.
    4.     private AudioSource[] sirens;                                       // Reference to the AudioSources of the megaphones.
    5.    
    6.    
    7.     void Awake ()
    8.     {
    9.         // Setup the reference to the alarm light.
    10.         alarm = GameObject.FindGameObjectWithTag(Tags.alarm).GetComponent<AlarmLight>();
    So it's instantiating a variable called "alarm" of the AlarmLight class (that's a script from an earlier lesson), then using FindGameObjectWithTags to find an object reference, and populating "alarm" with that reference.

    As far as i understand, this makes the add component panel utterly redundant. I don't see any reason this wouldn't work if the object in question had no added components at all. It's linking up an instance of a script, and an instance of an object.

    Am i horribly misunderstanding this here?
     
  2. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
    It would seem so. :) It's not instantiating anything, it's just grabbing a reference to the AlarmLight component that is on the GameObject it finds so that it can call methods on it or whatever.
     
  3. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,077
    Isn't that script meant to reference a different script in a different object? I.e., the script you showed doesn't actually have an AlarmLight of its own, but it needs some data from an AlarmLight script. It looks to me like it's setting up a reference to the AlarmLight script, probably so it can grab data from it.

    I.e., you have Object 1 with Script A trying to pull data from Object 2 with Script B. Object 1 with Script A needs a reference to Object2 with Script B in order to do something to Script B. Does that make sense or have I perhaps missed the point?
     
  4. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Code (CSharp):
    1. private AlarmLight alarm;  
    This line right here is instantiating an instance of the AlarmLight class, isn't it ?
     
  5. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
    Nope. That's a variable declaration.
     
  6. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    oooh my bad. I'm a bit rusty. I remember now, there's no new keyword in there.

    yes sorry this thread was silly, i understand now. thank you ^^