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 sgPwnZone · Jun 18, 2013 at 08:59 PM · javascripttogglepickuphowflashlight

help with Flashlight Pickup

First and formost- I need the coding to be in JAVASCRIPT not C SHARP

Hey Guys, I've read a couple other questions related to this, but they both involved batteries. I don't want batteries for the flashlight in my game. I just want for the player to be able to walk over to a table, and click E to pickup the flashlight. BUT I do want them to be able to turn it on and of with the F key. I already have the script for the flashlight turning on and off writen, I just dont now how to incorporate the whole PICKUP part off it where they wont me able to turn it on until they pick it up. This is what I have so far.

private var lightSource : Light; var soundTurnOn : AudioClip; var soundTurnOff : AudioClip;

function Start () { lightSource = GetComponent(Light);

}

function Update () {

if (Input.GetKeyDown(KeyCode.F)) ToggleFlashLight();

}

function ToggleFlashLight () {

lightSource.enabled=!lightSource.enabled;

//Audio if (lightSource.enabled) {

audio.clip = soundTurnOn;

} else {

audio.clip = soundTurnOff;

}

audio.Play();

}

@script RequireComponent(AudioSource) @script RequireComponent(Light)

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

Answer by Jeejo · Jun 18, 2013 at 09:35 PM

For picking up the flashlight, I would do something like this:

 var worldModel : GameObject;
 var viewModel : GameObject;
 var actualLight : Light;
 var hasLight : boolean = false;
 
 function OnTriggerEnter (other : Collider) {
     if(other.name == "player name here") {
         hasLight = true;
         Destroy(worldModel);
         Destroy(this);
     }
 }
 
 function Update () {
     if(hasLight == true) {
         viewModel.active = true;
         actualLight.enabled = true;
     }
     else {
         viewModel.active = false;
         actualLight.enabled = false;
     }
 }

As for turning it on and off, I would make another script.

 var actualLight : Light;
 var on : boolean = true;
 var soundTurnOn : AudioClip;
 var soundTurnOff : AudioClip;
 
 function Update () {
     if(Input.GetKeyDown("f")) {
         on = !on;
         if(on == true) {
             audio.PlayOneShot(soundTurnOff);
         }
         else {
             audio.PlayOneShot(soundTurnOn);
         }
         
     }
     if(on == true) {
         actualLight.enabled = true;
     }
     else if(on == false) {
         actualLight.enabled = false;
     }
 }
Comment
Add comment · Show 8 · 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 sgPwnZone · Jun 18, 2013 at 09:43 PM 0
Share

what do I assign each of those scripts to? The player? The Spotlight?

avatar image Jeejo · Jun 18, 2013 at 10:00 PM 0
Share

Assign the first script to the trigger, and assign the second script to the player.

avatar image sgPwnZone · Jun 18, 2013 at 10:02 PM 0
Share

Ok, well I got it to where the player walks into the trigger and the flashlight vanishes and the spotlight turns on, but there are a few changes I would like to make. The "toggle flashlight" scipt isnt turning it on and off for some reason, and I want to make it to where the player presses a button to pickup the flashlight, so its not done automatically

avatar image sgPwnZone · Jun 18, 2013 at 10:05 PM 0
Share

Ok Ok, Now I have it to where the toggle works, but you can make the on and off sounds before you pick up the flashlight, how to do i change this?

avatar image Jeejo · Jun 18, 2013 at 10:29 PM 0
Share

Sorry about late response, but you can access the different scripts by doing something like this:

 var player = GameObject.Find("player name here");
 var onOff = player.GetComponent(2d script name here);
 
 if(hasLight == true) {
     onOff.enabled = true;
 }
 else {
     onOff.enabled = false;
 }
Show more comments

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

15 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

Related Questions

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

Toggle button help 1 Answer

Flash light toggle problem 1 Answer

FlashLight Fixes Please?? 1 Answer

How to pickup and equip an object? 0 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