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 /
  • Help Room /
avatar image
0
Question by mrlafle · Dec 01, 2016 at 05:36 AM · javascriptcounterincrementlivesitem pickup

Having Trouble Incrementing an Integer

Hi, I'm trying to make a breakout clone using Javascript. There's an item that needs to add one extra life when the player collects it, but it's not incrementing right at all. It doesn't add one life during the current play session, but it adds one after I stop running the game and play again, as if incrementing how many lives the player should start the game with next time the game runs. It's hard to explain, but apparently somebody had the exact same problem six years ago: UnityAnswers

Here is my code, simplified:

 //This script is attached to the item//
 
 var ball:GameObject;
 var paddle:GameObject;
 
 function OnTriggerEnter (c:Collider) {
 
         //I'd like either the ball or paddle to collect the item
     if(c.gameObject.tag == "ball" || c.gameObject.tag == "paddle"){
         ball.GetComponent(BallScript).balls++;
         Destroy(gameObject);
     }
 }

.

 //This script is attached to the ball//
 
 var balls:int;  //basically "lives"
 
 function OnCollisionEnter(col:Collision){
     if(col.gameObject.tag == "floor"){
         balls--;   //this works exactly how it should
         ResetBall();
     }
 }

If anyone has had/solved this issue before, I'd really appreciate some help!

Comment
Add comment · Show 2
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 hexagonius · Dec 01, 2016 at 10:47 AM 0
Share

right now only the current ball has lives. If it dies, the value is gone. If you want to keep it make the variable static, OR save lives in a different script that stays during the whole game

avatar image mrlafle hexagonius · Dec 01, 2016 at 01:02 PM 0
Share

Hi hexagonius, I am not destroying the ball when it "dies", but just resetting it back in front of the paddle.

1 Reply

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

Answer by mrlafle · Dec 01, 2016 at 01:49 PM

I don't really post about my bugs here on Unity Answers to get help from anyone. Instead, I rely on the fact that I will inevitably find the solution myself shortly after posting, and that's what happened here :)

So I think the bug occurred because I assigned the "ball" and "paddle" variables on the Item prefab in the Project panel, and not the instance in the Scene. I was also assigning the ball and paddle prefabs instead of their corresponding instances in the scene.

To fix this, I hid the variables in the inspector for simplicity, and I assigned those variables using this on the Item's script:

 function Start () {
     ball = GameObject.FindWithTag("ball");
     paddle = GameObject.FindWithTag("paddle");
 }

This does not seem like a very common issue whatsoever, but if anyone thinks they're having a similar problem, I hope they find this helpful :)

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

109 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 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

Help..... please..... how do I do this? 0 Answers

Save current time when Game Over, then compare to new current time and get the difference? 0 Answers

How to make a GUI bar that counts items collected 0 Answers

Killed enemies counter 4 Answers

Increase variable? 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