Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by djluna · Oct 17, 2016 at 07:23 PM · flashlight

I need help with a Flashlight script

I have been trying to get my flashlight script to work yet, it keeps saying there is no light attach to the game object.

Here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class Flashlight : MonoBehaviour
 {
 
     public float a;
     public float b;
     public float c;
     public float d;
 
     public int batLevel;
 
     public Light Flashlight;
     public bool isOn;
     public float timer;
 
     void Start()
     {
         Flashlight = GetComponent<Light>();
         batLevel = 101;
         minusBat();
         isOn = true;
     }
 
     void minusBat()
     {
         if (isOn)
         {
             batLevel -= 1;
         }
     }
 
     void Update()
     {
 
         if (timer >= 0)
         {
             if (isOn)
             {
                 timer -= Time.deltaTime;
             }
         }
 
         if (timer <= 0)
         {
             timer = 5;
             minusBat();
         }
 
         if (Input.GetKeyUp(KeyCode.F))
         {
             Flashlight.enabled = !Flashlight.enabled;
 
             if (!isOn)
             {
                 isOn = true;
             }
             else
             {
                 isOn = false;
             }
 
         }
 
         if (batLevel == 0)
         {
             batLevel = 0;
             Flashlight.enabled = false;
             isOn = false;
         }
 
     }
 
     void OnGUI()
     {
         GUI.Box(new Rect(0, Screen.height / 1.21f, Screen.width / 6.16f, Screen.height / 19.58f), batLevel.ToString());
     }
 }

can someone tell me what i can do to fix this?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by doublemax · Oct 17, 2016 at 09:31 PM

 Flashlight = GetComponent<Light>();

If this finds no Light, it usually means that there is no Light component. Is the script attached to the light?

 public Light Flashlight;

As this is public, did you drag a light (from another GameObject) into this variable? Then you must remove the line from above, because it will overwrite the content.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image djluna · Oct 18, 2016 at 07:21 PM 0
Share

The script is not attached to the Light component, but to a modeled object that has the Light as a child, there is no light in the variable.

avatar image doublemax djluna · Oct 18, 2016 at 08:13 PM 0
Share

The script is not attached to the Light component, but to a modeled object that has the Light as a child

In that case GetComponent can't find the light. Use GetComponentInChildren ins$$anonymous$$d.

https://docs.unity3d.com/550/Documentation/ScriptReference/GameObject.GetComponentInChildren.html

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flashlight toggle 5 Answers

Light Probes - Need Help with one curious flashlight problem 0 Answers

Can anybody help me with a flashlight toggle script? 0 Answers

Flashlight script help! 2 Answers

how can I make my flashlight move with mouse movements 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges