using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
//setting up the score to show in game
public static int scorevalue = 0;
private static int scorevaule;
Text score;
private int currentlivesvalue;
void Start()
{
score = GetComponent<Text>();
}
void Update()
{
score.text = "Score " + scorevalue;
if (Score.scorevaule == 100)
{
currentlivesvalue += 1;
}
}
}
the if statement didn’t work I even tried to use greater than or equal to.
Here’s the current lives script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//setting up current to show in-game
public class CurrentLives : MonoBehaviour
{
public static int currentlivesvalue = 3; //starts the game with 3 lives
Text currentlives;
void Start()
{
currentlives = GetComponent<Text>();
}
void Update()
{
currentlives.text = "Lives " + currentlivesvalue;
}
}
Then you have a function that adds points to the current score:
void AddPoints( int points)
{
score += points;
// and now we give lives for passing the bonus interval
if (score >= bonus)
{
lives += 1;
bonus += bonusInterval;
// todo: maybe play a "Bonus Life!" sound here
}
}
You will have to adapt it to however you want to break your classes down, but the above will work every time and it is extremely simple.
Thanks! should I make a brand new script for it? So I have one that shows and sets the score and current lives but then a third script for the extra lives?
The computer scientists love to go nuts with a million different classes, but I prefer to keep it nice and simple and just put ALL the above code into whatever you want to call your “Game Manager” script.
If you don’t already have a Game Manager-ish kinda script, you might enjoy the benefit of having a single central class that controls the highest-level progress in the game: score, lives, bonuses, what level to play next, powerups, etc.
All of those things are almost guaranteed to be VERY tightly-intertwined in any given game, so I like to keep them all close to each other in a single script.
Thanks for the advice! I am just starting off on game developing so its a relatively simple game. So like there’s no power ups, no fancy animations, no fancy graphics, visual effects are very basic etc…
Yeah, definitely don’t let yourself get bogged down by too much concern for breaking things apart into small reusable parts (the entire point of classes). Just go with what works best for you because you will soon see what benefits might come from breaking certain things up. And when you see that, go back and break it up! That’s why we call it ‘software,’ because it is soft and can be changed.
Right now I just made a new script for it because I still get confused when everything is part of the same script lol. I made the script but which object do I attach it too? my score or current lives object? or the object that you collect to get the extra life?
What object is on doesn’t matter. These scripts could be static classes if you want since they don’t rely on Unity plumbing.
If it is a separate script and not static, it now needs a reference to the script storing the score, and the script storing the lives.
Also, not sure if an “extra lives” script should also have the .AddPoints() function in it, so now you can see precisely why multiple classes can paralyze you with indecision until you are comfortable with how to deal with this complexity.
If you’re calling that method from another script/class, make sure the ‘AddPoints’ method is public.
If you need to ‘get’ the current score from elsewhere, you can make that public, as well, or use a property or a method to return the value of the variable.
When you’re adding to the score, you also have to make sure you have a reference to that ExtraLife script.
One question, though: When you say it’s not working, do you have any errors? Did you debug (debugger/Debug.Log) to see what part of the script was/wasn’t working. Any other info to add?
It was late at night so I rushed my post so I can get to bed then after I went to bed I remembered what I also wanted to say My score does show up and it does go up when its supposed to and the current lives shows up and losing a life works but getting the extra life doesn’t work. I can try what you suggested here.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ItemCollected : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "Player")
{
Destroy(gameObject);
Score.scorevalue += 100; //score increases by 100 after collecting it
}
}
}
And this one gets the score to show up in-game, I put in a text object from the UI:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//setting up the score to show in game
public class Score : MonoBehaviour
{
public static int scorevalue = 0; //starts the score at 0
private static int scorevaule;
Text score;
void Start()
{
score = GetComponent<Text>();
}
void Update()
{
score.text = "Score " + scorevalue;
}
}
Okay, so with those scripts present, it looks as though the ExtraLife script has its own variables, and is not referring to the ‘Score’ script, the way it’s written.
If you gain your score when you collect items, you should call the method to AddScore.
You can reconsider putting the code in the same script, or if not that, then make sure they have the references they need (and look up the right score to increase/show, etc).
I would imagine you’d want it when you enter the trigger? That looks like how you’re adding to the score, but you are not calling the method that was proposed to you earlier for adding lives. You are using your original code, and the new script isn’t being used.