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

Noob Unity user keeps loosing references in scripts

Discussion in 'Scripting' started by Kandy_Man, Jun 20, 2015.

  1. Kandy_Man

    Kandy_Man

    Joined:
    Mar 8, 2014
    Posts:
    59
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Fade : MonoBehaviour {
    5.  
    6.     private Light fadeLight;
    7.     private bool direction;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         fadeLight = GetComponent<Light>();
    12.         direction = true;
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.         if (fadeLight.intensity == 0)
    18.         {
    19.             direction = true;
    20.         }
    21.         else if (fadeLight.intensity == 8)
    22.         {
    23.             direction = false;
    24.         }
    25.    
    26.         if(direction)
    27.         {
    28.             fadeLight.intensity += 0.1f;
    29.         }
    30.         else if (!direction)
    31.         {
    32.             fadeLight.intensity -= 0.1f;
    33.         }
    34.     }
    35. }
    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.
     
  2. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    ah that's because in the Start() method you set
    Code (CSHARP):
    1. 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
     
    topherbwell and Kandy_Man like this.
  3. Kandy_Man

    Kandy_Man

    Joined:
    Mar 8, 2014
    Posts:
    59
    My god thank you. I can't believe it was that simple.

    So GetComponent<T>() would be used if I wanted to access a component in my current GameObject rather than a different one, right?
     
  4. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    yes precisely, you can use it to get components on other objects as seen below :

    Code (CSHARP):
    1. GameObject myObj;
    2.  
    3. void Start()
    4. {
    5.    myObj.GetComponent<T>();
    6. }
     
    Kandy_Man and topherbwell like this.
  5. Kandy_Man

    Kandy_Man

    Joined:
    Mar 8, 2014
    Posts:
    59
    I thought using GetComponent<T>() in the Start() method retrieved components from the current object, not other objects?
     
  6. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    In the example from 5vStudios above, he references another gameobject and calls GetComponent<T>() on that object. GetComponent<T>() is a method from the Base GameObject class, so naturally all objects that inherit from that class can use GetComponent<T>().
     
    Kandy_Man and 5vStudios like this.
  7. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    In the example I provided :
    I made reference to another GameObject 'myObj' and called GetComponent<T>() on that object, so the component will be retrieved from the referenced GameObject 'myObj' rather than the object that the script is attached to
     
    Kandy_Man likes this.
  8. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    Lol welcome to scripting. where the most simple mistake will haunt your nightmares.
     
    Kandy_Man and 5vStudios like this.
  9. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    Haha, Well Said
     
    Kandy_Man likes this.
  10. Kandy_Man

    Kandy_Man

    Joined:
    Mar 8, 2014
    Posts:
    59
    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.