Scripting Changes to Unity 5

I’m doing the project stealth tutorial after doing the survival shooter, and it seems that there was a major change as to how to access the components and object properties the script is attached to.

Here’s what they call for in code:

public class LaserBlinking : MonoBehaviour
{
    public float onTime;            // Amount of time in seconds the laser is on for.
    public float offTime;           // Amount of time in seconds the laser is off for.
   
   
    private float timer;            // Timer to time the laser blinking.
   
   
    void Update ()
    {
        // Increment the timer by the amount of time since the last frame.
        timer += Time.deltaTime;
       
        // If the beam is on and the onTime has been reached...
        if(renderer.enabled && timer >= onTime)  //*****
            // Switch the beam.
            SwitchBeam();
     }
//I left the rest out)

The line where I added the “//*****” my "enabled’ is in RED highlighting an error. Do I have to define the object that the script is attached to as a private variable first in order to access the renderer? I’ve seen stuff like this a lot with the previous tutorials, but I haven’t seen anyone explain what was changed so that their script works fine as is, but extra steps are needed?

Replace renderer with GetComponent()

thanks! that worked, but I’m really interested in what was changed between unity 4.x - 5 to make the script logic not work the same so I understand better.

This is the case for all component shortcuts (renderer, rigidbody, collider, etc)

1 Like

–Eric

thank you, this is exactly what I was looking for! Thanks for the replies everyone