Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by penguinburger1 · Jun 21, 2015 at 11:20 AM · javascriptgetcomponentlightbooleanenable

Why is my gameObject.Find("Spotlight").GetComponent.() not working?

The script is for a light switch that when triggered with "e", is supposed to turn on a spotlight in the scene. It's not turning on though.

I disabled the light in the scene so I could turn it on with my lightswitch in game. However, even though I get no Error message, it just isn't enabling.

Any suggestions?

EDIT

I've just found out that when I'm in game, I can't enable the Light checkbox, even in the inspector... so the script isn't letting me enable it.

SpotlightSwitch script

 var open : boolean = false;
 
 var openAnimationString : String;
 var closeAnimationString : String;
 
 var buttonTransform : Transform;
 var distToOpen : float = 2;
 
 @HideInInspector
 var playerTransform : Transform;
 @HideInInspector
 var cameraTransform : Transform;
 
 var openSound : AudioClip;
 var closeSound : AudioClip;
 
 function Awake () {
     playerTransform = GameObject.FindWithTag("Player").transform;
     cameraTransform = GameObject.FindWithTag("MainCamera").transform;
     if(open){
         GetComponent.<Animation>().Play(openAnimationString);
     }
 }
 
 function Update () {
 
 //my getComponent variable.
     var myLight : Light = gameObject.Find("Spotlight").GetComponent.<Light>();
 
     var alreadyChecked : boolean = false;
     var angle : float = Vector3.Angle(buttonTransform.position - cameraTransform.position,
 buttonTransform.position + (cameraTransform.right * buttonTransform.localScale.magnitude) -
 cameraTransform.position);
     if (Vector3.Distance(buttonTransform.position, playerTransform.position) <= distToOpen){
     if (Vector3.Angle(buttonTransform.position - playerTransform.position, cameraTransform.forward) < angle){
     if (Input.GetKeyDown("e") && !GetComponent.<Animation>().isPlaying){
 
 //Here is my enable/disabling below.
 
         if (open){
             GetComponent.<Animation>().Play(closeAnimationString);
             open = false;
             alreadyChecked = true;
             myLight.enabled = false;
             if (closeSound){
                 GetComponent.<AudioSource>().PlayOneShot(closeSound);
                 }
         }    
         if (!open && !alreadyChecked){
             GetComponent.<Animation>().Play(openAnimationString);
             open = true;
             myLight.enabled = true;
             if (openSound){
                 GetComponent.<AudioSource>().PlayOneShot(openSound);
             }
         }
 
 //First attempt.
 
         /*if (open){
             myLight.enabled = true;
             }
         if (!open){
             myLight.enabled = false;
             }*/
     }
     }
     }
 }

(This script is the same script I'm using for my door, I just got lazy and didn't change the script to say "hit" or something instead of "open" and "close".

alt text

capture.png (435.3 kB)
Comment
Add comment · Show 2
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 Socapex · Jun 21, 2015 at 12:45 PM 0
Share

Just a quick note, in Unity 5, you need to use setEnable(false). Now I haven't been through your whole code, but that could be an issue.

avatar image penguinburger1 · Jun 21, 2015 at 01:01 PM 0
Share

Interesting. Before making the light switch object, I had the toggle set to a key, just in its own script, and it worked. Looks like this:

 var on : boolean = false;
 
 function Update(){
 
     if(Input.Get$$anonymous$$eyDown("f")){
     
         on = !on;
     
     }
     
     if(on){
         GetComponent.<Light>().enabled = true;
         
         }
         
     if(!on){
         GetComponent.<Light>().enabled = false;
 
         }
 }

And I just finished trying what you said. I get an error message now that says, "$$anonymous$$issing$$anonymous$$ethodException: UnityEngine.Light.setEnable"

my code snippet looks like this (did I write it incorrectly?):

 if (open){
             GetComponent.<Animation>().Play(closeAnimationString);
             open = false;
             alreadyChecked = true;
 //Here
             myLight.setEnable(false);
             if (closeSound){
                 GetComponent.<AudioSource>().PlayOneShot(closeSound);
                 }
         }    
         if (!open && !alreadyChecked){
             GetComponent.<Animation>().Play(openAnimationString);
             open = true;
 //and Here
             myLight.setEnable(true);
             if (openSound){
                 GetComponent.<AudioSource>().PlayOneShot(openSound);
             }
         


Big thank you for your attention! I appreciate it.

3 Replies

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

Answer by penguinburger1 · Jun 21, 2015 at 03:25 PM

Got it. I had the lightToggle script (that I commented) still attached, and its default was off, so because that was off, I never could turn it on with the main script.

Comment
Add comment · Show 1 · 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 penguinburger1 · Jun 21, 2015 at 06:41 PM 0
Share

Again, thank you all for your attention, concern, help, advice, and all that. It means a lot that you'd try to help me out with this!

avatar image
0

Answer by iceicetonne · Jun 21, 2015 at 03:25 PM

maybe it works when you change your myLight variable : var myLight : Light = gameObject.Find("Spotlight").GetComponent.();

to something like: public var myLight : GameObject = gameObject.GetComponent.();

in inspector you should assign your light you want to turn on/off

Comment
Add comment · 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
0

Answer by Maethorion · Jun 21, 2015 at 06:08 PM

If I'm not mistaken, gameObject.Find() only finds gameobjects that are enabled, so if your light is disabled when your script tries to find it, it might be null. Not sure if that's the problem then

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to enable/disable Javascript using a C# script? 1 Answer

Can someone translate C# to Javascript? 2 Answers

enable and disable boxcollider (whats wrong with my script?) 2 Answers

Deactivate children with .active = false 2 Answers

disable/enable Mouse Look(c#) with a script of js 7 Answers


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