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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[solved] Flashlight help

Discussion in 'Scripting' started by xDest, Jul 12, 2015.

  1. xDest

    xDest

    Joined:
    Jul 12, 2015
    Posts:
    4
    Hi, this is my first day using Unity, and my first day using C# (I find it similar to Java).

    I was wondering how I could get a specific spotlight object for my script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class flash : MonoBehaviour {
    5.  
    6.     public Light fl;
    7.     // Use this for initialization
    8.     void Start () {
    9.         //    fl = GameObject.GetComponent<Light>;
    10.         fl = GetCompontent("flash") as Light;
    11.     }
    12.  
    13. }




    This is what I have so far but I can't seem to get the object D: what do I do ;-;

    **This is also my first post so I'm not sure if this is in the right spot
     
    Last edited: Jul 12, 2015
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    you could try to fix a point light or something to your game object & then turn it on & off via script as needed
     
  3. xDest

    xDest

    Joined:
    Jul 12, 2015
    Posts:
    4
    yeah I have the point light under the object in Hierarchy but I just don't know how to reference it from the code :L
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I think if you put something like this on your light it should work


    Light myLight = lightPoint.GetComponent("Light");

    if (Input.GetButtonDown(//whatever you want to use to turn it on)) { myLight.enabled = true; }

    I don't have access to check the code though
     
  5. xDest

    xDest

    Joined:
    Jul 12, 2015
    Posts:
    4
    lightPoint doesn't exist ;-; im srry im seriously totally lost
     
    Last edited: Jul 12, 2015
  6. xDest

    xDest

    Joined:
    Jul 12, 2015
    Posts:
    4
    idk what i did but it works now
     
  7. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    I suggest you read into the unity documentation. Look up some basic tutorials. All that good stuff.

    For example, here's the scripting reference for GetComponent. It shows you how to use it in code.

    Here's the page with all the official unity tutorials on it. Some of them may be outdated, so you may need to post again with some questions in the future. I'd be happy to help out a beginner in any way I can.