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 /
avatar image
0
Question by $$anonymous$$ · Feb 05, 2018 at 11:07 PM · variablemultipleupdatingdecreaseanother script

Variable decreases multiple times

Hello, everyone! I have two scripts in different gameobjects. First one (short version)

 public class GriffController : MonoBehaviour {
 
 public int lifeCount = 4;
 
    void OnCollisionEnter2D(Collision2D target) {
 
         if (target.gameObject.tag == "DownTarget") {
             lifeCount = lifeCount - 1;
 
         }
 }
 }

Second one (short version)

 public class EggController : MonoBehaviour {
 
     public GriffController gf;
 
     float lastTime = 0;
     public float speed = 0;
 
     public void Update() {
 
         if (Input.GetMouseButtonDown(0)) {
             speed = Time.time - lastTime;
             lastTime = Time.time;
         }
 
         if (Input.GetMouseButtonDown(0) && speed < 0.2f && speed != 0) {
 
             gf.lifeCount = gf.lifeCount - 1;
    
         }
     }
 
 }

In the GriffController script, I reduce the lifeCount var when I collide with an object every 3 seconds.

In the EggController script, I check how often the player clicks on the mouse. If he does this often, I want to reduce the variable lifeCount, which is instantiated in the GriffController script.

I need this variable to change in both cases, but now the behavior is: I click very quickly, but the variable does not decrease. As soon as the condition from the first script is fulfilled, the variable is reduced by the number of clicks that I managed to make in three seconds and additionally - 1 as in GriffController.

How to make the variable decrease in addition when the player clicks too fast?

Comment
Add comment · Show 1
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 Buckslice · Feb 06, 2018 at 12:14 AM 0
Share

I think I mostly understand your problem. I tried both your scripts in a fresh project and the EggController clicking worked and was able to edit the GriffControllers lifeCount value. But it seems like for you the value is only updated when the GriffController is hit in a collision? If you have only given us the short version of your scripts I am guessing the problem is elsewhere within those scripts.

0 Replies

· Add your reply
  • Sort: 

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

79 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 avatar image avatar image avatar image

Related Questions

Variable not decreasing during runtime (C#) 1 Answer

Glitchy variable when updating through a function 2 Answers

JS Setting int value from another script 1 Answer

Adding a value to a variable every second 4 Answers

Decrease life from another script 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