Add left over bonus points to score

I have this bonus counter counting from 3500 to 0

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


public class BONUS : MonoBehaviour
{

    public float Bony = 3500;
    public Text Bonuss;

    private Roundsystem RS;

    void Start()
    {
        Bonuss = GetComponent<Text>();
        RS = GameObject.FindObjectOfType<Roundsystem>();
    }

    // Update is called once per frame
    void Update()
    {
        if (RS.runbonustimer == true)
        {
            Bony -= 5 * Time.deltaTime;
            Bonuss.text = Bony.ToString("BONUS" + "\n" + "00000");
     
        }
  
    }
}

How can i left over BONUS time add to the score that i have wich uses this code :

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

public class Scoring : MonoBehaviour
{
    public static int scoreValue = 0;
    public Text score;

    void Start()
    {
        score = GetComponent<Text>();
    }
    void Update()
    {
        score.text = scoreValue.ToString("SCORE" + "\n" + "000000");
    }
}

Thank you for your time as allways :slight_smile:

To add the rest of the time to the score, you need the info about the time in the Scoring script. If both scripts added on the same object, you can do:

public class Scoring : MonoBehaviour
{
    private BONUS _bonus;   
    void Start()
    {
        _bonus = GetComponent<BONUS>();
    }

    void Update()
    {
        score.text = scoreValue + _bonus.Bony;
    }
}

But i would set the field “Bony” to private and create a property with get + set for it and working with that property outside of the class.

Thanks i will toy with this and let you know how it went.

I get this error if i try your first solution :
Operator + cannot be applied operands of type method group and int.

Did you change anything on the scoreValue field (i think it doesn´t need to be static btw)?

1 Like

I had an error in the BONUS script that is corrected. How ever i get now the error cannot convert type float to string

this is what i have now :

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

public class Scoring : MonoBehaviour
{
    public static int scoreValue = 0;
    public Text score;
    private BONUS _bonus;

    void Start()
    {
        score = GetComponent<Text>();
        _bonus = GetComponent<BONUS>();

    }
    void Update()
    {

        score.text = scoreValue +_bonus.Bony;
        score.text = scoreValue.ToString("SCORE" + "\n" + "000000");
    }
}

And the bonus script :

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


public class BONUS : MonoBehaviour
{

    public float Bony = 3500;
    public Text Bonuss;

    private Roundsystem RS;

    void Start()
    {
        Bonuss = GetComponent<Text>();
        RS = GameObject.FindObjectOfType<Roundsystem>();
    }

    // Update is called once per frame
    void Update()
    {
        if (RS.runbonustimer == true)
        {
            Bony -= 5 * Time.deltaTime;
            Bonuss.text = Bony.ToString("BONUS" + "\n" + "00000");
      
        }
    

    }
}

Try this:

void Update()
{
    float totalScore = (float)scoreValue + _bonus.Bony;
    score.text = totalScore.ToString();
}

I think you have to cast you int anyway into a float too.

I am not a brilliant coder yet :smile: but it gives now the line Object reference not set to an instance of an object :slight_smile: Wat and how do you mean casting the in into a float ? Srry if i do not get it all right away :slight_smile:

int and float are different data types (int doesn´t allow decimal places), that´s why we cast (convert) the int type into a float type, that is done with the “(float)” infront of the int value (scoreValue).

For easier help, you should provide the whole error message (if possible with the line number). But in this case i think that wether “_bonus” object or the “score” object is null. But i guess, that the _bonus object is null. You can check that, if you put a “Debug.Log(_bonus)” in line 16. Did you add the BONUS script to the same gameobject as the Scoring script in the editor or are they both on different objects ?

Update: Forget the first part, you can do that without a cast, sry my bad. So you can just do:
float totalScore = scoreValue + _bonus.Bony

Thank you for taking the time to help me out :slight_smile: I am sure more users will find it handy if we can solve it as i see this question asked often but never really solved (At least not in their topics)

This is what i have now :

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

public class Scoring : MonoBehaviour
{
    public static int scoreValue = 0;
    public Text score;
    private BONUS _bonus;

    void Start()
    {
        score = GetComponent<Text>();
        _bonus = GetComponent<BONUS>();

    }
    void Update()
    {

        float totalScore = scoreValue + _bonus.Bony;
        score.text = totalScore.ToString();



        score.text = scoreValue.ToString("SCORE" + "\n" + "000000");
    }
}

The full error line is : NullReferenceException Object Reference is not set to an Instance of an object Scoring.Update() (at Assets/Scripts/Scoring.cs:21)

Yes ok, so your _bonus object is null. That means it can not be found in line 15/17 (GetComponent(); )

That´s the problem. The scripts are not on the same gameobject. The question is, do you wanna have them on the same gameobject or not ?

ahhhhh nope there not on the same game object :slight_smile: the scoring uses the UI text scoring and the bonus uses the UI text Bonus

Ok then you can do the following:

add in your Scoring script:

using UnityEngine.UI;

public class Scoring : MonoBehaviour
{
     public Text _bonusText;

     // all the other fields, which you have...

    void Start()
    {
        _bonus = _bonusText.GetComponent<BONUS>();   
     }
}

And drag in the inspector/scene your bonus ui text in that script as “_bonusText”

1 Like

No errors but i have to go for now i will try to figure it out and let you know.

1 Like

So i understand what you did but it does not solve anything. This simply creates a new variable that is totalscore but what i need is : bounus count stops at say 2300. Bonus points should be added to the scoreValue (other gameobject)

But i think because the scoreValue is a Int and the bonus is not somehow it’s not that easy to add to each other :slight_smile:

But totalScore is the result of the addition of your current score (scoreValue) and the time that is left (Bony). That´s what the line “float totalScore = scoreValue + _bonus.Bony;” is doing and after that you set the result (totalScore) to your text with “score.text = totalScore.ToString();”

You can add a “Debug.Log(totalScore);” 22/24 then you can see the result in the console output if you run the game

Yes it works as it should. I will see if we can put this in a clear nice script jacket to share. Thank you for your patience.

1 Like

One final question. The script works. But when bonus timer is reset the score value is going back to the score it was given before bonus was added. How come. After adding the bonus the new score should be the one at next round adding also the normal score points during the game (That uses add to scoreValue)

This is what i have now

using System.Collections;
using System.Collections.Generic;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.UI;

public class Scoring : MonoBehaviour
{
    public static int scoreValue = 0;
    public Text score;
    private BONUS _bonus;
    public float totalScore;
    private Roundsystem RS;
    public Text _bonusText;
    public bool addingscore = true;


    void Start()
    {

        RS = GameObject.FindObjectOfType<Roundsystem>();
        score = GetComponent<Text>();
        _bonus = _bonusText.GetComponent<BONUS>();

    }


    IEnumerator addingit()
    {
        score.text = totalScore.ToString("SCORE" + "\n" + "000000");
        yield return new WaitForSeconds(0.5f);
        _bonus.Bony = 4000;
       StopCoroutine(addingit());
    }


    void Update()
    {

        if (RS.bladcount == 0 && addingscore == true)
        {
            score.text = totalScore.ToString();

            totalScore = scoreValue + _bonus.Bony;
            StartCoroutine(addingit());
            addingscore = false;
}
        if (RS.bladcount > 0)
               {
         addingscore = true;
        score.text = scoreValue.ToString("SCORE" + "\n" + "000000");
        }
    }
}

Because you set the value of scoreValue again as score.text:
score.text = scoreValue.ToString("SCORE" + "\n" + "000000");

You have to update your scoreValue after the round is over. I´m not 100% sure, but i think you have to do in line 49:
scoreValue = (int)totalScore;

if your totalScore can have some decimal places, you have to do:
scoreValue = (int)Math.Round(totalScore);

That should set the totalScore of the last round to your new current score

Super i did not understand how to make it do that but no I do :slight_smile: All solved and working thank you for helping me and if i look at the views many others with this thing :slight_smile: