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 rdmartyr7 · Feb 20, 2015 at 12:28 PM · c#player settings

My player lives section of my code is not functioning properly

I'm working on a game for my portfolio class and I have poured over the forums and answers to get assistance with my issue. I'm now posting my question to get some answers from the community.

I have a destroy by contact script for my hero character, but I added a couple lines that would give the player a certain amount of lives and when he dies, the level would restart and when he reaches zero lives, then the game over screen would come up. The problem is that the player restarts over and over again but the game over screen never comes up when the player should be game over.

I got some of the script pieces from the forum, I've tried using Random.Range but then I get an error saying that I can't turn an integer into a boolean.

using UnityEngine; using System.Collections;

public class DbyContact : MonoBehaviour {

 public GameObject explosion;
 public GameObject playerExplosion;
 public int scoreValue;
 public int lives;
 private GameController gameController;

 void Start ()
 {
     GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
     if (gameControllerObject != null)
     {
         gameController = gameControllerObject.GetComponent <GameController>();
     }
     if (gameController == null) 
     {
         Debug.Log ("cannot find 'GameController' script");
     }

     }
     
     void OnTriggerEnter(Collider other) {
         if (other.tag == "Boundary")
         {
             return;
         }
         if (other.tag == "pBullet") 
         {
             return;
         }
         if (other.tag == "eBullet")
         {
             Instantiate(explosion, transform.position, transform.rotation);
         lives -=1;
         if (lives<=3)
         {
         Application.LoadLevel (Application.loadedLevel);
         }
         if (lives<=0)
         {
             Application.LoadLevel ("GameOver");
     }        
         if (other.tag == "Enemy") 
         {
             Instantiate (explosion, other.transform.position, other.transform.rotation);
         lives -=1;
             if (lives<=3)
             {
                 Application.LoadLevel (Application.loadedLevel);
         if (lives<=0)
         {
                 Application.LoadLevel ("GameOver");
         //Loser.Restart ();
     }
             }
     //if (lives <= 0) 
     //{
     //    Application.LoadLevel ("GameOver");
     //}
     gameController.AddScore (scoreValue);
     Destroy(other.gameObject);
     Destroy(gameObject);
     }
 }
 }
 }
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 rdmartyr7 · Feb 20, 2015 at 12:28 PM 0
Share

Thank you for the quick response tanoshimi. After reading that my question would be better stated, how do I get the player to restart the current level and maintain the updated life count?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by siaran · Feb 20, 2015 at 04:47 PM

I can think of 3 basic ways to solve this, each with their own issues.

Option Number One is to call DontDestroyOnLoad on your player, and then set him at the start of the level once ith as been loaded. The problem here is that if you start with a player existing in your level, there will be 2 players when the level is reloaded, so you will have to think of something to resolve that.

The second option is to not reload the entire level but instead 'reset' it. You'll have to keep a reference to what all starting values and stuff are, though.

The final (probably simplest) option is to store your player's number of lives in either playerprefs or a static class, and load that value into your player's prefab in its Start() method. Here you mostly just have to be careful when and how you set it to it's initial (maximum) value.

Comment
Add comment · Show 1 · 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 rdmartyr7 · Feb 25, 2015 at 03:05 PM 0
Share

Siaran,

Thank you for those options, I'll look up how to do those and see which one works the best

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

2 Apple Mach-O Linker Errors (see edit #3) 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 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