Noob Unity user keeps loosing references in scripts

Hi everyone, I’ve been looking to get started in Unity ever since Unity 5 was announced, before then I only really had, for lack of a better term, “experience”, with UDK through my University course.

I’m trying to start out simple with scripting. I love that Unity uses C# as that’s what I’ve been learning for the past 2 years at uni so I figured I’d get something going pretty quickly.

I have the following script:

using UnityEngine;
using System.Collections;

public class Fade : MonoBehaviour {

    private Light fadeLight;
    private bool direction;

    // Use this for initialization
    void Start () {
        fadeLight = GetComponent<Light>();
        direction = true;
    }
   
    // Update is called once per frame
    void Update () {
        if (fadeLight.intensity == 0)
        {
            direction = true;
        }
        else if (fadeLight.intensity == 8)
        {
            direction = false;
        }
   
        if(direction)
        {
            fadeLight.intensity += 0.1f;
        }
        else if (!direction)
        {
            fadeLight.intensity -= 0.1f;
        }
    }
}

All it’s doing is making the light pulse using the intensity variable. This works fine when attached directly to a light.

However, if I make fadeLight public and try to attach the script to an object, say a cube, and reference this light like below:

Where “Point light” is written changes to “None” as soon as I hit play, and returns as soon as I quit playing. If I drag the light source over during play, the script works as intended.

Basically, why does it lose the reference? Every Unity video I’ve seen on scripting tells me this should work.

Thanks to anyone who can help, I’ve felt like a right tool for the last week or two.

ah that’s because in the Start() method you set

fadeLight = GetComponent<Light>();

what that line does is attempt to set ‘fadeLight’ to the light component added to the gameObject that your script is added to

you can either simply remove that line or setup an if statement so that it only runs if ‘fadeLight’ is null, depends on your situation

2 Likes

My god thank you. I can’t believe it was that simple.

So GetComponent() would be used if I wanted to access a component in my current GameObject rather than a different one, right?

yes precisely, you can use it to get components on other objects as seen below :

GameObject myObj;

void Start()
{
   myObj.GetComponent<T>();
}
2 Likes

I thought using GetComponent() in the Start() method retrieved components from the current object, not other objects?

In the example from 5vStudios above, he references another gameobject and calls GetComponent() on that object. GetComponent() is a method from the Base GameObject class, so naturally all objects that inherit from that class can use GetComponent().

2 Likes

In the example I provided :

I made reference to another GameObject ‘myObj’ and called GetComponent() on that object, so the component will be retrieved from the referenced GameObject ‘myObj’ rather than the object that the script is attached to

1 Like

Lol welcome to scripting. where the most simple mistake will haunt your nightmares.

2 Likes

Haha, Well Said

1 Like

Wow I feel pretty dumb right now lol. You wouldn’t think I’d done C# at uni for 2 years and still overlook things like that lol

Thanks guys that all really helped a lot.