[Solved] Issue with Displaying score to player.

i am trying to create a space shooter game, I have issue with displaying score.
So in this script I am trying to Display score to player, But issue is that the score does not increase when IncreaseScore Function is called.
I am Calling Increasescore function from another script, which calls function when an Meteor (Which is object which we have to destroy) is destroyed.
This script is attached to empty game object in scene.

  • Sorry for bad english and i am a Very beginner to scripting and unity*

The Script.

public class ScoreSystemScript : MonoBehaviour {

    public int Score;
    public Text ScoreText;

    void Start ()
    {
        Score = 1;
        ScoreDisplay();
    }
 
 
    void Update ()
    {
       ScoreDisplay();
    }
 
    public void Increasescore ()
    {
        Score = Score + 5;
    }

    void ScoreDisplay()
    {
        ScoreText.text = "Score: " + Score.ToString();
    }
}

Function is called from this Script
```csharp
** public GameObject Meteorr;
private MeteorFall Metfallscript;
private GameObject ScoreSys;
private ScoreSystemScript Scoresyssc;

void Start()
{
    Metfallscript = Meteorr.GetComponent<MeteorFall>();
    Scoresyssc = ScoreSys.GetComponent<ScoreSystemScript>();

   
}
void OnCollisionEnter2D(Collision2D bulld)
{
    if(bulld.gameObject.tag == "Finish")
    {
        Destroy(bulld.gameObject);
        Destroy(this.gameObject);
        Metfallscript.IncreaseSpeed();
        Scoresyssc.Increasescore();
    }
}

}**
```

Do you have any errors?
Is the second script on a game object in the scene?
Are the game objects that you reference in the scripts part of the scene (not prefabs)?
Is the rest of the code working, eg: the collision?

I suggest that you modify your script by removing ‘Update’ and the score would look like this:

public void Increasescore ()
{
  Score = Score + 5;
  ScoreDisplay();
}

That won’t fix your problem, but it just makes more sense.

Thanks for replay,
Yes, Following error.

OnBulllethit is script from which the function of increasing score is called (second script)

And yes! Second script is attached to bullet which my plane shoots!

In Meteorr’s OnCollisionEnter2D method, the last two lines aren’t getting called because you are destroying the gameobject, which also destroys the script. Destroy the game object last and see if that works.

Nope!
First Line is getting called. Only Second isnt.
if i put the second line to top, then object dosent even destroy!

I think that doesn’t actually matter in this case, as the code below will still run before it’s destroyed.

What you have to do is assign the relevant game object to the script when you instantiate it into the game.
That way its reference will be correct and valid. Store the game object reference you need on the script that spawns your game object.

Well, if i assign my ScoreingScript to a gameObject, then i cannot assign the text in text field!

I’m not sure what you’re doing, but I have a different question.

Do you instantiate the bullets that you fire? And the 2nd script you posted in on those bullets?

Yeah!
Actually I Have attached 2 script sto bullet Prefab one which is posted above (2nd script in first post), and another which makes it move as soon as its instantiated, and another script to spawn point (an empty gameobject which is at top of my plane) which instantiates the bullet, so as soon as Bullet is instantiated it starts moving!

YES! 2nd script is attached to those bullets (Bullet Prefab)

Okay, so what I’m suggesting is that you make a variable on the script that spawns the bullet, and add this:

public ScoreSystemScript scoreSystem;

Also, change the one on your script to be public.

Now, when you spawn the bullet, it might look like this:

GameObject bullet = Instantiate(bulletPrefab);
bullet.GetComponent<BulletScript>().Scoresyssc = scoreSystem;

Sorry, I don’t know the name of your script, just change it so the name is right. :slight_smile:

You cannot make a reference to a scene object from a prefab/asset. So, this way that I’m suggesting is that you make a reference to your score system, inside the script that does the spawning (and is in the scene, also). You pass said reference to the spawned bullets.


Take a loook at this image.

also Did as you said :slight_smile:

If that’s on a prefab, you’re not allowed to do that.

Did you understand what I said before that you create a variable on the script that spawns the bullets and pass it to the bullets when you spawn them?

    public ScoreSystemScript scoreSystem;
    public GameObject Bullet;
  

    void Start ()
        {
       
        }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {

            GameObject BulletTemp = Instantiate(Bullet, transform.position, transform.rotation) as GameObject;
            BulletTemp.GetComponent<OnBullethit>().ScoreScript = scoreSystem;
       
           

        }

I dont know correct or No but i attached it by that way!

Okay, great. You’re almost done. Just remove this from your script (not needed)

private GameObject ScoreSys;

and remove the the part in Start() that gets the score system (the old way). Then it should be working, you can test it. :slight_smile:

Sadly nope :frowning:

This error yet persists.

 void OnCollisionEnter2D(Collision2D bulld)
    {
        if(bulld.gameObject.tag == "Finish")
        {
            Destroy(bulld.gameObject);
            Destroy(this.gameObject);
            ScoreScript.increaseScore(); //This line is giving error
            Metfallscript.IncreaseSpeed();

I Guess i will have quit working on this thing all togethar, i might have messed up project :frowning:

Well, one thing to ask… did you drag n drop the score system to the variable on the script that spawns the bullets?

1 Like

I Cannot thank you @methos5k enough for saving my ass :slight_smile:
Thank you sooo much sir!
i was about to scrap my project. You Sir are hero! :slight_smile:

Oh, I was in the middle of responding that you might want to check where it’s being called. :slight_smile:

I’m glad that you fixed it.

No worries, glad I could help you out. Enjoy the rest of your project. Take care :slight_smile:

1 Like

@methos5k
Sorry to bother you again.
Can you explain or Link me to any tutorial, where i can understand What this line does, and why we added it?

 BulletTemp.GetComponent<OnBullethit>().ScoreScript = scoreSystem;

Thanks in advance

which part? I mean, I can explain the entire line here… You’re using the variable that was assigned when you instantiated the new game object ‘BulletTemp’. You’re getting its component ‘OnBulletHit’ and accessing its variable ‘ScoreScript’ to which you assign the score system. :wink: lol.

Why we did this was so your newly spawned bullets could have a reference to a game object in the scene. Since you can’t reference a scene object on the prefab itself, this was a way to communicate the needed data (variable) to it.

You can check out many scripting tutorials here: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

Hope that helps :slight_smile: