• 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 mitendewking · May 27, 2015 at 11:21 AM · textenemycounterlivescollsion

How to fix lives counter in C#

so with in my project there is an object that lowers the score by ten and the lives by one but every time this collision occurs the score automatically goes to zero please help the entire code is as follows

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour { public float speed; public GUIText countText; public GUIText winText; public GUIText livesText; private int lives; private int count; private Transform cameraTransform;

 void start ()
 {
     count = 0;
     SetCountText ();
     lives = 10;
     SetLivesText ();
     winText.text = "";
 }
 void FixedUpdate ()
     {
     //The ball will move horizontal when pressing  the side arrow keys
             float moveHorizontal = Input.GetAxis ("Horizontal");
             float moveVertical = Input.GetAxis ("Vertical");
     //When the ball is moving "horizontal" it is moving on the x axis, when the ball is moving "vertical" it's moving on the Z axis
             Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         //The ball which is a rigidbody object will be moved when a certain amount of force is added to it    
     rigidbody.AddForce (movement * speed * Time.deltaTime);
     }
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "PickUp")
     {
         //Every time the ball collides with a cube, the counter will add one
         other.gameObject.SetActive(false);
         count = count + 1;
         SetCountText ();
     }
     if (other.gameObject.tag == "Capsule")
     {
         //Every time the ball collides with a capsule, the counter will add two
         other.gameObject.SetActive(false);
         count = count + 2;
         SetCountText ();
     }
     if (other.gameObject.tag == "diamond")
     {
         //Every time the ball collides with a diamond, the counter will add five
         other.gameObject.SetActive(false);
         count = count + 5;
         SetCountText ();
     }
     if (other.gameObject.tag == "Enemy")
     {
         //Every time the ball collides with a icosphere, the counter will subtract one life and 10 points
         other.gameObject.SetActive(false);
         lives = lives + -1;
         SetLivesText ();
         count = count + -10;
         SetCountText ();
     
     }
 

 }
 void SetLivesText (){
             livesText.text = "lives:" + count.ToString ();
             if (lives <= -1)
     {
                     winText.text = "GameOver";
         livesText.text = "Lives: 0";
                     if (audio.isPlaying) {
                             audio.Stop ();
             
                     }

             }
     }
 void SetCountText ()
 {
     //When the counter reaches fourty, the "you win" message will pop up
     countText.text = "Count: " + count.ToString ();
     if(count >= 40) 
     {
         winText.text = "YOU WIN!";
         if (audio.isPlaying){
             audio.Stop();

         }
     }
 }

}

the main issue lies with the Enemy game object and the lives counter

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 tanoshimi · May 27, 2015 at 09:39 PM

Not sure what value you expected it to have, but void start() should be void Start(), for one thing.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Wave script not working... 1 Answer

Lose one life when player hits the water 1 Answer

Why the text and the UI code is can't applying 0 Answers

transporting player to gameobject when it collides with different gameobject 0 Answers

TextMesh counter in real-time 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