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;
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:
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.
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.
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)
…
Sorry, I don’t know the name of your script, just change it so the name is right.
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.
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. 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.