Ok this course is my first time scripting so bear with me. I got this exercise to work fine by copying your script. But ...

Ok this course is my first time scripting so bear with me. I got this exercise to work fine by copying your script. But I don't know why it works... So we create a public variable that is a light? I don't quite understand that cause in the intro course you talked about variables being values like integers or bools etc. But in this case the variable is a component? So the Light is the variable and we assigned "lightBulb" as the descriptor. Then on start we say lightBulb equals the light component. That seems like a circular loop to me and sort of redundant. We define the light component variable as "lightBulb" then we still have to tell it that "lightBulb" equals the Light component? Sorry that's the best I can articulate my question, any help would be appreciated.
  • Jonathan Gonzalez(jgonzalez) replied

    A component is essentially just a script. So we can access specific functions/variables made public within that script as long as we make a reference to it within our script. So intensity and range are both specific to the light component and we can access them. 

    When we use GetComponent we’re telling Unity to cache that specific component so we can access parts of that component. Note that previously Unity would have certain component that could be accessed without using GetComponent but in general it's a good idea to use it to cache it yourself. 

     It would be similar if we had our own custom script and we wanted to access things within that script from an external script. Hope that makes sense.

  • andrewm2211 replied
    It does help, Thank you. I guess my follow up would be.. What is the purpose of saying lightBulb= Light Instead of defining lightbulb as the Light component why not just reference Light when adjusting the intensity etc.?
  • Jonathan Gonzalez(jgonzalez) replied

    I think I understand what you mean. Instead of creating a variable then using GetComponent you want to access it directly. Unity won’t let you do that with certain components (mostly custom ones). There are some components Unity caches for you automatically. 

    Usually these are the most commonly used ones. For the Light you could use it directly, although Unity sometimes goes back and forth on which components can be used this way. 

    So hence why you may see Rigidbody using GetComponent and sometimes it's not used. With Light it's the same way and things like Transform. 

    For anything custom, like a script you wrote you'll need to use GetComponent as Unity doesn't know what that script contains. Using GetComponent regardless won't do any harm if it's in the Start function as you're telling Unity that you want to cache it once within the start of that function rather than having Unity do it. 

  • andrewm2211 replied
    ok thanks, starting to make sense. probably going to do this course twice
  • Jonathan Gonzalez(jgonzalez) replied
    I'd recommend that. There are plenty of things that make more sense once you've been through it a few times. Even I start to piece things together a lot easier once I've come back to it after gaining some experience. Feel free to ask any questions and I'll do my best to answer them.