- Home /
multiply Score on Collision not working? check my code plz
So what I'm trying to do here is when the player collide By " x2" object the score gets update +2 instead of +1 , I tried a lot of codes and I can't get anything to work, maybe its easy and I miss something, please check my code and guide me here
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class UIManager : MonoBehaviour{
public static bool gameOver;
public Text scoreText;
int score;
bool x2= false;
// Use this for initialization
void Start () {
gameOver = false;
score = 0;
InvokeRepeating ("scoreUpdate", 1.0f, 0.5f);
}
// Update is called once per frame
void Update () {
scoreText.text = "Score: " + score;
}
public void scoreUpdate() {
if (gameOver == false) {
if (x2 == false) {
score += 1;
}
if (x2 == true) {
score += 2;
}
}
}
public void gameover(){
gameOver = true;
}
void OnTriggerEnter2D (Collider2D col) {
if (col.gameObject.tag == "x2") {
Destroy (col.gameObject);
x2 = true;
float timer = 5.0f;
timer -= Time.deltaTime;
if (timer <= 0) {
x2 = false;
}
}
}
}
the scores only update +1 and never multiply when collision is happens.
public static bool gameOver;
public Text scoreText;
int score;
bool x2= false;
float timer = 0f;
// Use this for initialization
void Start () {
gameOver = false;
score = 0;
InvokeRepeating ("scoreUpdate", 1.0f, 0.5f);
}
// Update is called once per frame
void Update () {
scoreText.text = "Score: " + score;
timer -= Time.deltaTime;
if (timer <= 0) {
x2 = false;
timer = 0f;
}
}
public void scoreUpdate() {
if (gameOver == false) {
if (x2 == false) {
score += 1;
}
if (x2 == true) {
score += 2;
}
}
}
public void gameover(){
gameOver = true;
}
void OnTriggerEnter2D (Collider2D col) {
if (col.gameObject.tag == "x2") {
Destroy (col.gameObject);
x2 = true;
timer = 5.0f;
}
}
Answer by cstooch · Jun 23, 2017 at 10:40 PM
I haven't been able to locate where your problem is, but I'm not sure what you're trying to accomplish with this though.. as far as I can tell this should really never do anything except if your frame is extremely slow. I'll try and step my way through the code in my head here, but I didn't see an issue. What I'd recommend YOU do though is put some Debug.Log outputs or something in your collision check though just to see if it's reaching that code (i.e. a collision is detected - make sure one object has a rigidbody if using collision)
float timer = 5.0f;
timer -= Time.deltaTime;
if (timer <= 0) {
x2 = false;
}
Edit: yah I've looked through the logic, and I think it should work, I'd just highly recommend you put a Debug.Log right after where you set x2=true and see if that code is ever reached... if not, there's your issue (most likely you're doing something wrong with colliders).
well the "x2" object gets destroyed when collides so it means it works and I did the Debug.log and the collision occurs fine. but it just keeps invoke repeating the first condition which is score +=1
I tested a very watered down version of this and it worked. So I don't know. The logic looks right to me (aside from that one part of code which I pointed out which shouldn't do anything, but that isn't a problem either).
I think what I'd do next is put some Debug.Log at the very start of your scoreUpdate and print out gameOver as well as x2.
I put a Debug.log after the if(x2 == true) to print (x2) and it does print true, it means it works, but the score is keep updating the same as +1. can you post the codes you tested?
Thanks man, it turns out you were right, I used the script twice, but I had to make some changes, now it works perfectly.
Answer by Guy_Yome · Jun 23, 2017 at 09:52 PM
If your collider for the 2x object is a trigger, it might not work. Or maybe you didnt assign the tag, just created it. Or you have a typo in the tag's name.
Also, you could have assigned the tag to the container of the object with the collider. $$anonymous$$ake sure the tag is on the one with it.
thanks for the replay, I unchecked the trigger and changed the Ontrigger to Oncollision, but thats not the issue because the collision works fine as the "x2" object gets destroy when the player collides but the score +=2 never get activated.
Could you Debug.Log the gameover variable. $$anonymous$$aybe you are always at gameover =true.
I think I know. The timer is initialized in the function. $$anonymous$$ake it a global variable. In the update you do the
timer -= time.deltaTime;
and if its 0 or under ( <= ) reset x2 to false and then make the timer = 0.
if (timer <= 0){ x2 = false; timer = 0; }
now when the collision enters make the timer 5.0f
timer = 5.0f;
You check if its 0 or under in the update function too.