Search Unity

Bug Bug in a Coin Game Im trying to make

Discussion in 'Editor & General Support' started by prasitgupta1782, May 24, 2022.

  1. prasitgupta1782

    prasitgupta1782

    Joined:
    May 21, 2022
    Posts:
    20
    So I'm a beginner and I am getting this really annoying problem. Basically, my script was working perfectly, and then I made a color change to a cube that was totally not related to the script. Anyways I ran the game again, and that script stopped working. Can someone figure this out? Please help. This is really annoying. I think this is definitely a bug but I'm still attaching the code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CoinScript : MonoBehaviour
    6. {
    7.     public GameHandler GH;
    8.     public AudioClip coinSound;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         GH = GameObject.Find("Canvas").GetComponent<GameHandler>();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.  
    19.     }
    20.     private void OnTriggerEnter(Collider Other)
    21.     {
    22.         GH.coins++;
    23.         AudioSource.PlayClipAtPoint(coinSound, transform.position);
    24.         Destroy(gameObject);
    25.     }
    26. }
    27.  
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  3. prasitgupta1782

    prasitgupta1782

    Joined:
    May 21, 2022
    Posts:
    20
    Nope, that was a different problem with the same script and a stupid one tbh. I tried to delete that thread but I couldn't. So uhmm, if you can help please help with this problem too. In this problem, there is definitely no problem with the code, unlike in the previous post.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please describe the problem. Add Debug.Log statements in your methods to ensure they are actually being executed https://forum.unity.com/threads/tips-for-new-unity-users.701864/#post-5057741
     
  5. prasitgupta1782

    prasitgupta1782

    Joined:
    May 21, 2022
    Posts:
    20
    Debugging didn't help.
    So my game is sort of an endless runner with a sphere set as the gameObject. After my game was complete I just added color to the last cuboid in my game. Everything was fine but then I ran the game again, the sphere passed right through the coins, and neither did the score increase which was something that was perfectly fine earlier. I don't seem to understand what went wrong. Please suggest a way to fix this. I spent a lot of time on this game but when i thought it was completed the major part of the game went wrong.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Did you confirm this code is executing? Ensure this code is attached to a game object, show a screenshot of the Inspector window for the object, showing this script is attached. Please share your updated code along with the Debug.Log output, perhaps something like the following, right after line 22.

    Debug.Log("Coins = " + GH.coins.ToString());
     
  7. prasitgupta1782

    prasitgupta1782

    Joined:
    May 21, 2022
    Posts:
    20
  8. prasitgupta1782

    prasitgupta1782

    Joined:
    May 21, 2022
    Posts:
    20
    Please look into it.
     
  9. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Sorry, you would need to look into it! Debugging doesn't "solve" the problem, it only allows you, the developer, to find it. Then fix it. So what is the debug output when you run it? I believe you forgot to include the debug output. No need for Google drive, just paste the log output here. And what are your conclusions so far, is the code executing? Is the value of coins incrementing, or staying 0? Place this in Start(), just get something working, and go from there:

    Debug.Log("I am in Start!");
     
  10. prasitgupta1782

    prasitgupta1782

    Joined:
    May 21, 2022
    Posts:
    20
    Thank you for the time you are giving. It's really kind of you.:)
    As for the debug, it isn't showing any problem with CoinScript but in GameHandler it shows
    NullReferenceException: Object reference not set to an instance of an object
    GameHandler.Update () (at Assets/GameHandler.cs:15)
    I will also add the GameHandler code just in case.
    I think the code is executing because when i changed the code to just destroy the gameobject it worked but when I altered it to add score too, neither did the score increase nor did the object destroy.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameHandler : MonoBehaviour
    7. {
    8.     public Text CoinText;
    9.     public int coins = 0;
    10.  
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         CoinText.text = "Coins :" + coins;
    16.     }
    17. }
    18.  
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    JeffDUnity3D likes this.
  12. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Debug.Log wouldn't "show" a null reference exception. And again you didn't provide the Debug.Log output. I might also suggest that you 1) first identify what is null 2) find out why it's null and 3) fix it.
     
    Kurt-Dekker likes this.