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 GraviterX · May 30, 2015 at 08:59 PM · javascriptflashlight

Enable/Disable Scripts

Hey guys. I know this question has been asked a billion times, but I can't seem to find the info I need. I'm trying to make a script where when the Player clicks a flashlight, the flashlight activates the flashlight script attached to the player while the model that the Player clicks destroys itself. While the model is destroyed, the script doesn't activate. Any help?

 var Flashlight : Flashlight;
 
 function Start() {
     Flashlight = GetComponent<Flashlight>();
 }
 
 function OnMouseDown() {
     Flashlight.enabled = true;
     Destroy(gameObject);
 }
Comment
Add comment · Show 3
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 Johnz1234 · May 30, 2015 at 09:03 PM 0
Share

what u want to destroy the flashlight or the script

avatar image Johnz1234 · May 30, 2015 at 09:13 PM 0
Share

post image or something

avatar image chubshobe · May 31, 2015 at 11:43 AM 0
Share

Can i see the Flashlight script?

3 Replies

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

Answer by JSierraAKAMC · May 31, 2015 at 12:57 AM

 var flashlight : Flashlight;
  
  function Start() {
      //GetComponent<Type>() is used in C#, while GetComponent(Type) is used in JS
      flashlight = GameObject.FindWithTag("Player").GetComponent(Flashlight);
  }
  
  function OnMouseDown() {
      flashlight.enabled = true;
      //Also, this line will cause troubles
      Destroy(gameObject);
  }


I found two errors in the code. The first is the GetComponent() call. It was an easy fix. The second problem that you would run into is that your Flashlight component would end up being destroyed along with the GameObject it's attached to. flashlight is a component on the GameObject (according to the code) and Destroy(gameObject) is called, destroying the GameObject and the components. However, you stated that you would like the Flashlight component on the player to be enabled, correct? If so, you should tag the player as "Player" or something similar and change the line flashlight = GetComponent(Flashlight); to flashlight = GameObject.FindWithTag("Player").GetComponent(Flashlight); Hope it works!

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 GraviterX · May 31, 2015 at 05:18 PM 0
Share

It's not giving me the errors, however, it won't let me click the flashlight gameobject and destroy it.

avatar image GraviterX · May 31, 2015 at 09:13 PM 0
Share

Never$$anonymous$$d I tinkered with some things and fixed it. Thank you so much for your help!

avatar image
0

Answer by awplays49 · May 30, 2015 at 09:16 PM

Hi @GraviterX,

Try changing the Flashlight variable representative to a lower case "f". Unity is probably getting your variable confused with the component. Is the word Flashlight (variable) a different color? If so, then problem solved :)

 var flashlight : Flashlight

EDIT

Also, always use lower case letters for first words of custom variables. If you don't, at some point it could be the same name as a script or something already taken, then you'll have to change everything.

Also, it would be great if you would accept this answer if it helped :)

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 GraviterX · May 31, 2015 at 12:27 AM 0
Share

It didn't change anything, but thanks for the advice I forgot to mention that I'm getting these errors (sorry, I forgot to check the console for probably the most important reason why it was failing) -(4,47): BCE0043: Unexpected token: ). -(4,48): BCE0044: expecting ), found ';'. -(4,49): UCE0001: ';' expected. Insert a semicolon at the end.

avatar image DiegoSLTS · May 31, 2015 at 01:51 AM 0
Share

This doesn't happen, the compiler is smart enough to know when to use the type and when to use the instance. If the method or member is static then it will asume you wanted to use the type, and if it's not static it'll asume you're using the instance.

The only time it doesn't work is if your variable is called like a class, but it's not a variable of that class and you'll get a compiler error if (and only if) the variable type doesn't have the method or member you're trying to access.

If you mix them (trying to access a static method or variable with an instance or trying to access non-static with the type) you'll get a compiler error.

The last part about starting variables always with lowercase it's hardly an "always do this" thing, it isn't even neccesary. If for some reason you end up with a variable in a script called like a new class you created, the code will still work just like before.

avatar image
0

Answer by DiegoSLTS · May 31, 2015 at 01:30 AM

This line:

 Flashlight = GetComponent<Flashlight>();

Should look like this:

 Flashlight = GetComponent.<Flashlight>();

if you want to use the generic version of GetComponent. Note the dot after GetComponent. With this you won't get those compiler errors.

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

24 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

Related Questions

GUI Texture Transparency issue 1 Answer

How to access the light component on a camera object? 1 Answer

Could anyone help me with .js code to be able to pickup a flashlight and turn it on and off -1 Answers

Setting Scroll View Width GUILayout 1 Answer

How do I draw back an inactive item ? 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