Unity & CSharp - Null Reference Error Help
Hey guys, I'm pretty new to C# (about a week lol) and have been following a ton of Unity tutorials and am working on creating my own small project.
The concept is simple, click on the bomb that's falling, it goes up, and you get a point. It was going well until I tried adding the score mechanic in. I have tried placing the score increments in different places, I've googled a lot and haven't found anything that helps. I created one of the Unity projects (Roll A Ball) following one of their tutorials, and it has a similar mechanic. Even checking against that code, I can't figure out what's wrong.
I have an empty named GameController that has the main GameController script on it. I have bombs that spawn that get the Bomb script on them.
This is the error I'm getting: NullReferenceException: Object reference not set to an instance of an object
GameController.setTheFuckingScore () (at Assets/GameController.cs:30)
GameController.isClicked () (at Assets/GameController.cs:41)
GameController.Update () (at Assets/GameController.cs:24)
Here is my code for the GameController. Also, I have the UI "Score Text" Element set in Unity on the Game Controller Script. (Not sure if I'm explaining this right, here's what I mean: https://prnt.sc/nxj8ee )
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
public Text scoreText;
private int score;
public bool clickedOn;
// Start is called before the first frame update
void Start()
{
score = 0;
setTheFuckingScore();
clickedOn = false;
}
// Update is called once per frame
void Update()
{
isClicked();
}
// Sets the score
void setTheFuckingScore()
{
scoreText.text = "Score: " + score.ToString();
}
public void isClicked()
{
if( clickedOn )
{
// < make the score work
clickedOn = false;
Debug.Log("I got clicked on!");
score = score + 1;
setTheFuckingScore();
}
}
}
Here is my code for the Bomb Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bomb : GameController
{
public Rigidbody rb;
public float t = 0.0f;
private bool moving = false;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
}
/// <summary>
/// OnMouseDown is called when the user has pressed the mouse button while
/// over the GUIElement or Collider.
/// </summary>
void OnMouseDown()
{
this.rb.velocity = new Vector3(0, 15, 0);
clickedOn = true;
}
}
Any help would be appreciated! Mostly what I saw on google that solved this problem was that people didn't have the initial value set, which I do in the Start. Everything else works, clicking on a bomb makes it bounce up, and I get the Debug.Log. I appreciate any answers and any criticism! Like I said I'm very new so I'm not even sure if I'm heading in the right direction. This is just a hobby of mine so you can shit on me and I won't mind. Thanks!
Edit: The problem line, according to the error, is this(Inside setTheFuckingScore)
scoreText.text = "Score: " + score.ToString();
Posted Jun 4, 2019, 3:22 PM UTC