Search Unity

Scoreboard; something is not working

Discussion in 'Getting Started' started by revyinvy, Feb 19, 2017.

  1. revyinvy

    revyinvy

    Joined:
    Feb 19, 2017
    Posts:
    5
    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
    Code (csharp):
    1.  
    2. public class lvlManager : MonoBehaviour
    3. {
    4.     public Text scoreText;
    5.     int score;
    6.  
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.         score = 0;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void fixedUpdate ()
    16.    {
    17.  
    18.      
    19.         scoreText.text = "Score:" + score;
    20.              
    21.    }
    22.  
    23.  
    24.  
    25.     public void scoreUpdate()
    26.     {
    27.         score++;
    28.     }
    29.  
    30.  
    31. }
    32.  
    33. on my bulletctrl script i created the following
    34.  
    35.   void OnCollisionEnter2D(Collision2D other)
    36.     {
    37.         if (other.gameObject.CompareTag("enemy"))
    38.         {
    39.          
    40.             Destroy(other.gameObject);
    41.             Destroy(gameObject);
    42.             lm.scoreUpdate();
    43.  
    44.  
    45.         }
    46.     }
    47. }
    48.  
    The score does not update when my bullets destroyed the enemy.
    My enemy is a prefab, is that why it cant update?


    Thank you.
     
    Last edited: Feb 19, 2017
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    A few things.

    1. Please use code tags when posting code
    2. Unity's magic functions (Update, FixedUpdate, etc) are case sensitive. You are using 'fixedUpdate' when I assume you want 'FixedUpdate' so it is called each frame.
    3. You should use Update rather than FixedUpdate. FixedUpdate is primarily for dealing with physics.
    4. Even better, you should update the text value only when the value changes rather than every frame.
     
    Last edited: Feb 19, 2017
    JoeStrout likes this.
  3. revyinvy

    revyinvy

    Joined:
    Feb 19, 2017
    Posts:
    5
    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
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    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:
    Code (csharp):
    1. void Update()
    2. {
    3.     scoreText.text = "Score:" + score;
    4. }
    5.  
    6. public void scoreUpdate()
    7. {
    8.     score++;
    9. }
    You can do:
    Code (csharp):
    1. public void scoreUpdate()
    2. {
    3.     score++;
    4.     scoreText.text = "Score:" + score;
    5. }
     
  5. revyinvy

    revyinvy

    Joined:
    Feb 19, 2017
    Posts:
    5
    Hi this is my new codes.
    Score still do not update

    my lvlmanager
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class lvlManager : MonoBehaviour
    8. {
    9.     public Text scoreText;
    10.     int score;
    11.    
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.         score = 0;
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update ()
    21.    {
    22.  
    23.        
    24.        
    25.                
    26.    }
    27.  
    28.  
    29.  
    30.     public void scoreUpdate()
    31.     {
    32.        
    33.         score++;
    34.         scoreText.text = "Score:" + score;
    35.     }
    36.    
    37.    
    38. }
    39.  
    mybulletctrl
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class BulletCtrl : MonoBehaviour
    8. {
    9.     public Vector2 speed;
    10.     private lvlManager lm;
    11.    
    12.  
    13.     Rigidbody2D rb;
    14.  
    15.     // Use this for initialization
    16.     void Start ()
    17.     {
    18.        
    19.         rb = GetComponent<Rigidbody2D>();
    20.         rb.velocity = speed;
    21.         lm = GetComponent<lvlManager>();
    22.        
    23.      
    24.        
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update ()
    29.     {
    30.         rb.velocity = speed;
    31.      
    32.  
    33.     }
    34.    
    35.     void OnCollisionEnter2D(Collision2D other)
    36.     {
    37.         if (other.gameObject.CompareTag("enemy"))
    38.         {
    39.            
    40.             Destroy(other.gameObject);
    41.             Destroy(gameObject);
    42.             lm.scoreUpdate();
    43.  
    44.  
    45.         }
    46.     }
    47. }
    48.  
    49.  
     
  6. revyinvy

    revyinvy

    Joined:
    Feb 19, 2017
    Posts:
    5
    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?
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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<lvlManager>(). 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<lvlManager>() 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<lvlManager>() call with a call to GameObject.FindObjectOfType<lvlManager>(). 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. :)
     
    SarfaraazAlladin and Ryiah like this.
  8. revyinvy

    revyinvy

    Joined:
    Feb 19, 2017
    Posts:
    5
    Hi Joe,

    Thank you for your reply.
    I added the following like you mention and it worked.

    Code (csharp):
    1.  
    2. lm = GameObject.FindObjectOfType(typeof(lvlManager)) as lvlManager;
    3.  
    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
     
    JoeStrout likes this.