Score help C#

Can anyone point me in the right direction or help me out a bit? My point trigger is not adding up the score when i run through the trigger. Also i am trying to get it to multiply after running through 5 consecutive triggers in a row without hitting an object. Can anyone assist? Ive been stuck for awhile.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Score_System : MonoBehaviour {
   
    public static int playerScore = 0;        //This is the player's score.
    public int pScore = 10;               
   
    private GameObject scoreText;
   
    // Use this for initialization
    void Start () {
        scoreText = GameObject.Find("scoreNumber");
    }
   
    //COLLISIONS
    void OnTriggerEnter(Collider col)
    {   
        //If we collide with a trigger, increase player's score.
        if(col.gameObject.name == "Points")
        {
            Destroy(col.gameObject);
            playerScore += pScore;
        }
       
    }
   
    void OnGUI()
    {
        scoreText.guiText.text = playerScore.ToString();
    }
}
public static int playerScore = 0;        //This is the player's score
public int pScore = 10;

private int comboBuild = 0;

void Update()
{
if (comboBuild >= 5)
{
pScore = 25; //Whatever you want the increased points to be
} else {
pScore = 10;
}
}

void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pickup")) //Make sure the other object has the Pickup tag assigned.
{
other.gameObject.SetActive (false);
playerScore += pScore;
comboBuild += 1;
Debug.Log ("A pickup has been hit");
}
if (other.gameObject.CompareTag ("Obstacle")) //Make sure the other object has the Obstacle tag assigned.
{
comboBuild = 0;
Debug.Log ("An obstacle has been hit");
}
}

Let me know how that works out for you. Also make sure that both the player object, as well as pickups and obstacles have “Is Trigger” checked.

@Gerald_Tyler for some reason i cant get the trigger to add points when i hit it. IsTrigger is selected everywhere, and not even a debug pops up … Ive been stuck here for awhile.

So what exactly is happening when you collide with the other object? Is the player simply passing through it and the other object remains?

@Gerald_Tyler If i hit the obstacle in the way, it triggers the hit/stumble effect of the character. But if i hit the points trigger, nothing happens, no score adds on the screen.

All I can say is double check to make sure that that specific points object has the Pickup tag on it, and the points and the player both have “Is Trigger” checked.
Also if you’re doing it where one object is 2D and the other is 3D that can cause issues.
If those aren’t it, I’m afraid you’ll have to wait for somebody more knowledgeable than I to come assist.

Also, make sure you have a Rigidbody on the object you want to detect entering the trigger, otherwise you won’t get the trigger call.

You may also want to put an extra Debug.Log() outside of the one that tests for particular tags, so you can tell if it is even getting into ANY of the OnCollisionEnter() function calls.

@Gerald_Tyler @Kurt-Dekker Appreciate all the help. So its wierd, if i add the tag Player to my script instead of pickup. The score adds, but my player disappears once they hit the trigger.

What do you have the script on? If it is on the object then the tag of the other object will be player & you are also setting the other object to false ie the player. If the score is working then where you have other.gameObject.SetActive=false; just remove the other from the start of it. That will set the object that the script is on to false.

If it is on the player then it should work as is but if it isn’t working uncheck isTrigger on the objects

@tedthebug appreciate it, got it going again, also appreciate everyone else’s help… Now, the score adds, but the score wont multiply with the combo script, any ideas?

public class Score_System : MonoBehaviour {
   
    public static int playerScore = 0;        //This is the player's score
    public int pScore = 10;
    private GameObject scoreText;
    private int comboBuild = 0;


    void Start () {
        scoreText = GameObject.Find("scoreNumber");
    }

    void Update()
    {
        if (comboBuild >= 2)
        {
            pScore = 20; //Whatever you want the increased points to be
        } else {
            pScore = 10;
        }
    }
   
    void OnTriggerEnter(Collider other)
    {
                if (gameObject.CompareTag ("Pickup")) { //object has the Pickup tag assigned.
                        gameObject.SetActive (false);
                        playerScore += pScore;
                        comboBuild += 1;
                        Debug.Log ("A pickup has been hit");
                }
                if (gameObject.CompareTag ("obstacle")) { //other object has the Obstacle tag assigned.
                        comboBuild = 0;
                        Debug.Log ("An obstacle has been hit");
                }
        }
void OnGUI()
{
    scoreText.guiText.text = playerScore.ToString();
}
}

Put a debug log into your update so you can see in the console what pscore is set to. Once you have collided with 2 objects pause the game & see what the value is. That will show if the pscore is being set correctly & may help narrow it down as it looks ok.

You could also try moving the gui score line from OnGui into your trigger lines if it is only updated on collision with pickups etc

Perhaps if the comboBuild +=1 were before the playerScore += pScore;

Because actually currently I think it’s adding the score, and then increasing the combo, so it looks like it would require 3 pickups to see an increase rather the 2 you’re wanting.

Also what I gave you is just changing the value, not a multiplication. For a multiplication you’d need to do something different.

You could just try doing
comboBuild +=1;
playerScore += pScore * comboBuild;

@Gerald_Tyler thx buddy appreciate the help your giving, been stuck here for a while. So the points are adding now, debug is showing they are triggering… Its just score is adding still when hitting an obstacle, and not multiplying. Ive been researching this all day, cant figure it out haha. The fun part of being a beginner at programming. So here is the updated, like you suggested.

public class Score_System : MonoBehaviour {
   
    public static int playerScore = 0;        //This is the player's score
    public int pScore = 10;
    private GameObject scoreText;
    private int comboBuild = 0;


    void Start () {
        scoreText = GameObject.Find("scoreNumber");
    }

    void Update()
    {
        if (comboBuild >= 1)
        {
            pScore = 20; //Whatever you want the increased points to be
        } else {
            pScore = 10;
        }
    }
   
    void OnTriggerEnter(Collider other)
    {
                if (gameObject.CompareTag ("Pickup")) { //object has the Pickup tag assigned.
                        gameObject.SetActive (false);
            comboBuild +=1;
            playerScore += pScore * comboBuild;
                        Debug.Log ("Point Triggered");
                }
                if (gameObject.CompareTag ("Obstacle")) { //other object has the Obstacle tag assigned.
                        comboBuild = 0;
                        pScore = 0;
                        Debug.Log ("No Points");
                }
   
        }
void OnGUI()
{
    scoreText.guiText.text = playerScore.ToString();
}
}

Sorry I had meant to do that instead of the other thing, not in addition to.

public class Score_System : MonoBehaviour {

public static int playerScore = 0;
public int pScore = 10;
private GameObject scoreText;
public int combo = 0; //Changed to "Combo" and set to public so you can view while running game

void Start () {
scoreText = GameObject.Find("scoreNumber");
}

//Nothing in void update

void OnTriggerEnter(Collider other)
{
if (gameObject.CompareTag ("Pickup")) {
gameObject.SetActive (false);
combo += 1; //Changed to combo rather than combo build
playerScore += pScore * combo;
Debug.Log ("Points triggered");
}

if (gameObject.CompareTag ("Obstacle"))
combo = 0; //Resets the combo, but upon next pickup will gain +1 from the Pickup collider
Debug.Log ("Combo reset");
}
}

void onGUI()
{
scoreText.guiText.text = playerScore.ToString();
}
}
}

There shouldn’t be any reason why score increases when hitting an “Obstacle” object

Put a debug in & step through the code when it hits an obstacle to see at what point it is adding the score. Check the values as you go through then work backwards to see why it is doing it.