Hi unity,
i tried searching for my problem and could not find a solution to it.
I am relatively new to unity (1 week to be exact), so please forgive my noobness
My score board does not update it.
I tried creating a lvlmanager with the following codes
public class lvlManager : MonoBehaviour
{
public Text scoreText;
int score;
// Use this for initialization
void Start ()
{
score = 0;
}
// Update is called once per frame
void fixedUpdate ()
{
scoreText.text = "Score:" + score;
}
public void scoreUpdate()
{
score++;
}
}
on my bulletctrl script i created the following
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("enemy"))
{
Destroy(other.gameObject);
Destroy(gameObject);
lm.scoreUpdate();
}
}
}
The score does not update when my bullets destroyed the enemy.
My enemy is a prefab, is that why it cant update?
Thank you.
hi,
sorry about the code tag, didnt know about it. I have edited it.
I change to Updated instead of fixed updated. still does not work.
For no 4. , i dont really get what you meant. Sorry I am pretty new here
Post your new code. Itâs also a good idea to add Debug.Log statements to check where the code is getting stuck.
Instead of:
void Update()
{
scoreText.text = "Score:" + score;
}
public void scoreUpdate()
{
score++;
}
You can do:
public void scoreUpdate()
{
score++;
scoreText.text = "Score:" + score;
}
Hi this is my new codes.
Score still do not update
my lvlmanager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class lvlManager : MonoBehaviour
{
public Text scoreText;
int score;
// Use this for initialization
void Start ()
{
score = 0;
}
// Update is called once per frame
void Update ()
{
}
public void scoreUpdate()
{
score++;
scoreText.text = "Score:" + score;
}
}
mybulletctrl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletCtrl : MonoBehaviour
{
public Vector2 speed;
private lvlManager lm;
Rigidbody2D rb;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = speed;
lm = GetComponent<lvlManager>();
}
// Update is called once per frame
void Update ()
{
rb.velocity = speed;
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("enemy"))
{
Destroy(other.gameObject);
Destroy(gameObject);
lm.scoreUpdate();
}
}
}
If i would like to call my BulletCtrl script from lvlmanager such that
void OnCollisionEnter2D(Collision2D other) = =true and score to update by + 1
would it be better and how do i do it?
I suspect the problem is that youâre still a bit vague on what GetComponent does. And I suspect the other problem is that youâre not watching for errors in the Console (or at least, not reporting them here).
In your BulletCtrl script, line 21, youâre setting lm = GetComponent(). This looks for a lvlManager component on the bullet itself. I find it very hard to believe that your bullet prefab has a lvlManager component; and if it does, those lvlManagers certainly donât have a reference to the UI.Text of the score display, nor do they share a common score as you would want.
Instead, I would expect that you have just one lvlManager component in the scene, on just one GameObject that you put in the scene for that purpose. Because itâs in the scene, it can have a reference to the UI.Text (which also lives in the scene); and because thereâs only one of them, youâll have only one score instead of a score for each bullet.
But that means that calling GetComponent() from a script on the bullet will not find the lvlManager. It will look at all the other components on the bullet, and nope, not there. So it returns null. And then your lm.scoreUpdate() call on line 42 will fail with a null reference exception.
The âproperâ way to fix this is to make your lvlManager a singleton. But a much easier way, and probably good enough for starting out, is to just replace your GetComponent() call with a call to GameObject.FindObjectOfType(). This will search the scene for your lvlManager object, and return a reference to that, even though it lives on a different object.
Finally, when you post about a problem, âit doesnât workâ is never a sufficient description. How doesnât it work? It doesnât compile and run? Nothing happens when you expect it to happen? Errors appear in the console, and if so, what? Ask a better question, and youâll get better answers. 
2 Likes
Hi Joe,
Thank you for your reply.
I added the following like you mention and it worked.
lm = GameObject.FindObjectOfType(typeof(lvlManager)) as lvlManager;
i apologize as i am relatively new to this, i will try to post more details as possible the next time.
thank you for your wonderful help
cheers
1 Like