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 /
This question was closed Nov 21, 2021 at 02:49 PM by burchland2 for the following reason:

Too subjective and argumentative

avatar image
0
Question by burchland2 · Nov 18, 2021 at 02:24 AM · getlife

How to get multiple lives

I have a game where every time the main character passes a goal at a certain time, it earns five extra lives along with five hundred points. Also, when the character reaches 100 points, it earns a single extra life. But I do not know how to program the first part. In fact, every time I finish a scene after I get the 500 points, when I destroy an enemy or obtain more points, I earn yet another extra life. Here is my script so far. The source of the problem is there somewhere.

GameStatus.cs: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

 public class GameStatus : MonoBehaviour
 {
     public static int score = 0;
     public static int lives = 9;
     public int target = 100;
     public AudioSource myAudioSource;
     public AudioClip bell;
     public int bonusPoints = 500;
 
     // Start is called before the first frame update
     void Start()
     {
         myAudioSource = GetComponent<AudioSource>();
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
 
     void OnDestroy()
     {
         Debug.Log("GameStatus was destroyed.");
     }
 
     public void AddScore(int s)
     {
         score += s;
         if (score >= target)
         {
             AddLives();
             target += 100;
             myAudioSource.PlayOneShot(bell);
         }
     }
 
     public void AddLives()
     {
         lives++;
     }
 
     public void LoseLives()
     {
         lives--;
     }
 
     public void Bonus()
     {
                score += 500;
                if (score >= bonusPoints)
                {          
                           lives += 5;
                           target += 500;
                           myAudioSource.PlayOneShot(bell);
                }
     }
 }

PlayerController.cs:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 using System;
 using System.IO;
 
 public class PlayerController : MonoBehaviour
 {
     public Rigidbody2D myBod;
     public AudioSource myAudioSource;
     public int moveOnDelay = 4;
     public AudioClip crowd;
     public AudioClip cent;
     public AudioClip net;
     public AudioClip bell;
     public AudioClip splash;
     public AudioClip bat;
     public CountdownTimer ct;
     public Vector3 respawnPoint;
     private UnitManager gameUM;
     private HealthManager hm;
     public AudioClip takeSound;
     public int damageTaken = 1;
     public Transform firePoint;
     public GameObject ninjaStar;
     public GameStatus gs;
 
     // Start is called before the first frame update
     void Start()
     {
         myBod = GetComponent<Rigidbody2D>();
         ct = FindObjectOfType<CountdownTimer>();
         myAudioSource = GetComponent<AudioSource>();
         gameUM = FindObjectOfType<UnitManager>();
         hm = FindObjectOfType<HealthManager>();
         respawnPoint = transform.position;
         gs = FindObjectOfType<GameStatus>();
         if (GlobalControl.Instance.IsSceneBeingLoaded)
         {
             PlayerState.Instance.localPlayerData = GlobalControl.Instance.LocalCopyOfData;
 
             transform.position = new Vector3(
                             GlobalControl.Instance.LocalCopyOfData.PositionX,
                             GlobalControl.Instance.LocalCopyOfData.PositionY,
                             GlobalControl.Instance.LocalCopyOfData.PositionZ + 0.1f);
 
             GlobalControl.Instance.IsSceneBeingLoaded = false;
         }
         
         if (Input.GetKeyDown(KeyCode.Return))
         {
             Instantiate(ninjaStar, firePoint.position, firePoint.rotation);
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         float x = Input.GetAxis("Horizontal");
         Vector2 origin = Vector2.zero;
         
         if (Input.GetButtonDown("Jump"))
         {
             myBod.velocity = new Vector2(x * 5, 7f);
         }
         else
         {
             myBod.velocity = new Vector2(x * 5, myBod.velocity.y);
         }
 
         if (Input.GetKeyDown(KeyCode.Return))
         {
             Instantiate(ninjaStar, firePoint.position, firePoint.rotation);
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "coin")
         {
             int coinScore = 10;
             Destroy(other.gameObject);
             gs.AddScore(coinScore);
             myAudioSource.PlayOneShot(cent);
         }
 
         if (other.gameObject.tag == "obstacle" || other.gameObject.tag == "enemy")
         {
             HealthManager.HurtPlayer(damageTaken);
             myAudioSource.PlayOneShot(takeSound);
         }
 
         if (other.gameObject.tag == "FallDetector")
         {
             gameUM.LifeLost();
             myAudioSource.PlayOneShot(splash);
         }
 
         if (other.gameObject.tag == "bottle")
         {
             hm.FullHealth();
             myAudioSource.PlayOneShot(crowd);
         }
 
         if (other.gameObject.tag == "goal" && ct.countingTime <= 10f)
         {
             ct.myAudioSource.Stop();
             gs.Bonus();
             StartCoroutine("MoveToNextUnit");
         }
 
         if (other.gameObject.tag == "goal" && ct.countingTime > 10f)
         {
             int bonusScore = (int)(ct.countingTime) * 2;
             gs.AddScore(bonusScore);
             myAudioSource.PlayOneShot(net);
             StartCoroutine("MoveToNextUnit");
         }
 
         
     }

     public IEnumerator MoveToNextUnit()
     {
         yield return new WaitForSeconds(moveOnDelay);
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
     }
 }

Any assistance would be appreciated.

Sincerely, burchland2

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 KloverGames · Nov 18, 2021 at 01:33 PM 0
Share

There doesn't seem to be anything wrong with your script. What's the issue? Like what do you want to happen?

2 Replies

  • Sort: 
avatar image
0

Answer by KloverGames · Nov 18, 2021 at 01:30 PM

Just say lives += 1;

Comment
Add comment · Show 5 · 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 burchland2 · Nov 18, 2021 at 01:48 PM 0
Share

Tried that. (lives += 5). Still no success.

avatar image KloverGames · Nov 18, 2021 at 01:53 PM 0
Share

Are you calling your function from a button? If you aren’t executing your function from Update() or Start() or from a button then nothing is going to happen at all…

avatar image KloverGames · Nov 18, 2021 at 01:53 PM 0
Share

I think your problem is that you’re creating functions but not executing them

avatar image burchland2 KloverGames · Nov 18, 2021 at 02:53 PM 0
Share

I've tried calling my function through update, but then, when I destroy an enemy or obtain more points, I earn yet another extra life. Let me edit my original post.

Edit: I just edited the post. Please tell me what else I should do in order to fix this.

avatar image KloverGames burchland2 · Nov 18, 2021 at 02:57 PM 0
Share

You need an if statement to tell the system when to execute your function. You aren’t supposed to just place it in Update()

avatar image
0

Answer by Bassem102 · Nov 18, 2021 at 03:22 PM

Hello @burchland2 , You should Make some debug and see if this code here gets executed.

 if (other.gameObject.tag == "goal" && ct.countingTime <= 10f)
          {
              ct.myAudioSource.Stop();
              gs.Bonus();
              StartCoroutine("MoveToNextUnit");
          }

You probably have some wrong conditions or something.

Comment
Add comment · Show 5 · 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 burchland2 · Nov 18, 2021 at 04:10 PM 0
Share

It does execute, but after I earned the bonus points and lives, on the next stage, every time I earn more points, I gain yet another extra life. How should I fix this?

avatar image KloverGames burchland2 · Nov 18, 2021 at 04:11 PM 0
Share

You said “on the next stage”, are you loading up a new scene? Or is it all one scene?

avatar image burchland2 KloverGames · Nov 18, 2021 at 06:46 PM 0
Share

I'm loading up a new scene.

Show more comments

Follow this Question

Answers Answers and Comments

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

Lives dont minus correctly 1 Answer

Player Health Not Working? 4 Answers

NullReferenceException error, GameObject.Find. 1 Answer

Script for getting in and out of a tank 2 Answers

C# Parent-SubClass set inherited properties help 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