Score isn't able to be counted
Hi, I might be missing something really simple; what I'm trying to do is add points when the player collides with an object, and the points are allocated to an int from another script. Once the % timer switches to points (known in the script as "chaos" bc its a kid wrecking his toys), the player is able to collect points through collisions. But what's happening is as if the script doesn't even read the line of code scoreManager.score += chaosPoints;
at all. If the Debug.Log line is before, it reads fine; if the Debug.Log line is after, the line is never read. Help please! ):
Player script that is attached to the player:
public class PlayerScript : MonoBehaviour
{
ScoreManager scoreManager;
int chaosPoints = 100;
void Awake ()
{
scoreManager = GetComponent<ScoreManager>();
}
public void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("making contact!" + scoreManager.score);
scoreManager.score += chaosPoints;
}
}
ScoreManager script that is attached to the scoretext ui:
public class ScoreManager : MonoBehaviour
{
GrabBlock grabBlock;
PlayerScript playerScript;
public Text text;
public int percent;
public int score;
float percentTime;
public bool chaos;
bool blockCollide;
void Start()
{
text = GetComponent<Text>();
percent = 0;
score = 0;
blockCollide = false;
chaos = false;
GameObject block = GameObject.FindGameObjectWithTag("Block");
grabBlock = block.GetComponent<GrabBlock>();
playerScript = GetComponent<PlayerScript>();
}
void Update()
{
PercentTick();
VisibleText();
}
void PercentTick()
{
percentTime += Time.deltaTime / 2f;
if (percentTime >= 1f)
{
percent += (int)percentTime;
percentTime -= (int)percentTime;
}
if (percent == 100)
{
chaos = true;
}
}
void ScoreCalculation()
{
}
void VisibleText()
{
if (chaos == false)
text.text = percent + "%"; //BUG: Throws NullReferenceException.
if (chaos == true)
text.text = "Chaos: " + score;
}
}
Your answer
Follow this Question
Related Questions
Why is the this object referenced to itself? 1 Answer
The referenced script on this Behaviour is missing - on any new script 0 Answers
Attack delay not working correctly 1 Answer
How do I keep two UI text unity3d on the scene 2 Answers
Instatiated object not being referenced in Start function? 2 Answers