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
-1
Question by TheblindGhost · May 18, 2014 at 10:02 PM · colliderflashlightpick upbattery

Need help with a script to Pick up Batteries for a Flashlight type Lighter

I want the Player to be able to press "e" and pick up Lighter Fuel to expand the Lifespan of the Light. I have everything mostly set up, however I can't seem to get the Player to pick up the Fuel (even if it's On Collide only) The fuel Object doesn't do anything ingame Here is the script for the Lighter, it is attatched to the First Person Controller. The First Person Controller is tagged as "Player", it seems to work as it should.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(AudioSource))]
 public class MatchesController : MonoBehaviour
 {
     public Light light;
     public float PowerLevel = 1500.0f; //Fuel for Lighter
     public AudioClip clickSound;
     public AudioClip clickSound2;
 
 
     
     void Start ()
     {
         light.enabled = false; //Disables the Light by default
     }
     
     void Update ()
     {
         if(Input.GetKeyDown(KeyCode.F)) //If the F key is pushed
         {
             
             if(light.enabled == false){  //Check if the Light is disabled, if it is, enable it and play a sound
                 light.enabled = true;
                 audio.PlayOneShot(clickSound);
 
         }
         else                         //Else, disable to light and play another sound
         {
             light.enabled = false;
                 audio.PlayOneShot(clickSound2);
             
         }
     }
 
         if(light.enabled == true) { //decrease the Powerlevel if the light is on
             PowerLevel -= 1.0f;
             }
 
             if (PowerLevel <= 500) {              // decrease the light intensity if the PowerLevel falls beneath 500
                  light.intensity -= 0.0015f;
             }
 
         if (PowerLevel >= 501) {      //if the Powerlevel is higher than 500, restore Intensity.
             light.intensity = 0.8f;
 
         }
 
 
 
 
 
 
 
             
 
 
         if (PowerLevel <= 0) {                      //If the Powerlevel reaches Zero, disable the light and
         
                         light.enabled = false;      //set the PowerLevel to Zero
             PowerLevel = 0;
 
                 }
 
 
 
 
 
 
 
         }
         }
 
 
 



here is the script for The Fuel Object made by Olgo and modified so it works for my game, there is no press "e" option in here yet. It si atatched to the game object I want to use as the fuel that you can pick up for a lighter

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(SphereCollider))] 
 public class Battery : MonoBehaviour {
     
     void Start(){
         //get the Sphere Collider component of this Battery and make it a trigger.
         this.GetComponent<SphereCollider>().isTrigger = true;
     }
     
     //This function will run whenever another object with a collider encounters this object's collider.
     void OnTriggerEnter(Collider other){
         
         //Check to see of the GameObject that collides with this Battery is tagged as Player
         //For this to work, you will need to set your controllable GameObject to Player
         if(other.gameObject.tag == "Player") {
             
 
 
                 
                 other.GetComponent<MatchesController>().PowerLevel += 510.0f; //increase the Powerlevel by 510
         
         }     
     }
 }

I have tried a couple of hours now and I can't seem to find the error. I'm thankful for every help :)

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 getyour411 · May 18, 2014 at 10:41 PM 0
Share

Press 'E' to pickup/do something?

avatar image TheblindGhost · May 18, 2014 at 10:57 PM 0
Share

yeah, but that option is not that important, what's more important is, that for some reason the collider in the "Battery" script doesn't seem to collide with the rigidbody of the Player for some reason

avatar image TheblindGhost · May 19, 2014 at 11:00 AM 0
Share

I followed what you said and afterward I could walk over the object and get the desired outcome, but that isn't exactly what I wanted because I need the player to enter the collider at a certain range. After Recreating the object as a new Prefab, it worked! Thank you so much :). could you repost your comment as answer so I can check it as answered :)

1 Reply

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

Answer by Cherno · May 19, 2014 at 01:49 AM

Your battery object needs a collider set to Is Trigger = true, you player Object needs a collider and a rigidbody. Also, check you Physics Preferences if there are any layers involved that don't interact in the Physics matrix.

Try

 if(other.gameObject.CompareTag("Player")) {

instead of

 if(other.gameObject.tag == "Player") {

Also, comment-out the tag condition completely to see if any collision registers at all.

Lastly, add Debug.Log lines at each and every step to see what works and what doesn't.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Battery life after the game ends PROBLEM 1 Answer

Battery Pickup Regeneration 2 Answers

Need Help Programming... Please Help! Thanks :) 1 Answer

Adding 1 point after collison 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