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 HeroHusky · Jul 09, 2014 at 03:33 PM · gameupdatefunctionsimpleincrement

Would like a function to change a parameter once, after an "if" statement has been met.

I've tried using "function Update ()" but that updates every frame until the parameter changes from the if statement. I'm trying to make the lives increase by 1 when the score gets to a certain amount.

 static var score: int;
 static var lives: int;
 
 function Start () {
 score = 0;
 lives = 3;
 }
 
 function OnGUI () {
 GUI.Box (Rect (10,10,90,30), "Score:   "+score);
 GUI.Box (Rect (Screen.width - 100,10,90,30), "Lives:   "+lives);
 }
 
 function Update () {
 if (score == 20)
 {
     lives++;
 }
 }



I've looked up multiple ways to try and do this, but I'm a complete noob at javascript. I've heard to use coroutines, bool, and other things, but I don't even know where to start with those. I even read about using invoke, awake, and the like, but I need it to change when the score reaches a certain amount, not during a timeframe. Thank you in advance, and any links/tuts would be greatly appreciated as this is my first week scripting.

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 KiraSensei · Jul 09, 2014 at 03:35 PM 0
Share

I suppose another script accesses the "score" variable and increment it ?

What behavior do you exactly want ? When the score is 20, one more life, and then what ? the score starts over to 0 ? Or the score has to be 40 then to win another life ?

1 Reply

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

Answer by WhoRainZone1 · Jul 09, 2014 at 03:41 PM

There are several ways, the way I'd go is to add a new bool variable like this

 static var score: int;
 static var lives: int;
 var reachedMaxScore: bool;

And also modify your Update function this way

 function Update () {
 if (score == 20 && reachedMaxScore == false)
 {
 reachedMaxScore = true;
 lives++;
 }
 }


Cheers

Comment
Add comment · Show 3 · 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 HeroHusky · Jul 09, 2014 at 03:52 PM 0
Share

Thank you so much. It worked like a charm. I kind of figured I needed to use a boolean, but had no idea how to implement it. Thanks again. Also, the new variable "reached$$anonymous$$axScore" you added, that is automatically assumed to be false?

avatar image KiraSensei · Jul 09, 2014 at 03:56 PM 0
Share

Yes, but you should initialize it at false to be clear. But in that case the player can't win another life after that.

avatar image WhoRainZone1 · Jul 09, 2014 at 07:42 PM 0
Share

As $$anonymous$$iraSensei said, you should make it false in the Start() function to proper work.. I have a little sleep deficit due to heavy unity sessions, so I forgot to mention that. :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Update increment error (2 + 1 = 0?) 1 Answer

How to Update a score count going up and down 1 Answer

Update Function Work Around Question 2 Answers

Start, Awake, Update. Any other ways to call functions from an empty GameObject? 3 Answers

Script is causing immense lag, and I don't know what's causing it. 2 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