int not adding up and access from another script

Hi Everyone!

I’m making a little car game and I currently have a scene filled with objected(street lights,benches,etc). I want to give the player points for colliding with these objects and knocking them over. Here’s my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class triggerScript : MonoBehaviour {

public int GiveAmount = 10;
public int CurrentAmount = 0;
public string PointString;
public Text PointText;

private void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.tag == "Player") 
    {
        CurrentAmount = CurrentAmount + GiveAmount;
        Debug.Log("Add points");
    }
}
private void Update()
{
    PointString = CurrentAmount + "points";
    PointText.text = PointString; //nullreferenceexception
}
}

first:

I get a lot of NullReferenceExceptions on line 24. it does debug.log but the score doesn’t go up or doesn’t show up. i have assigned the text element with the one in-game.

Question 2: how can I access the currentAmount int from another script so i can add up the score?

All help is appreciated!

first make sure you have assigned the PointText in the inspector. I don’t think other reason for null reference exception.
You are updating your text in the update method which is not good technique because of performance issues.
Make UnityEvent and invoke every time when your score gets update ads in the script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class triggerScript : MonoBehaviour
{

    public int GiveAmount = 10;
    public int CurrentAmount = 0;
    public string PointString;
    public Text PointText;
    //Make unity Event
    public UnityEvent UpdateScore;

    private void Start()
    {
        UpdateScore.AddListener(delegate
        {
            PointString = CurrentAmount + "points";
            PointText.text = PointString;
        });
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            CurrentAmount = CurrentAmount + GiveAmount;
            //Invoke the every time when your score update
            UpdateScore.Invoke();
            Debug.Log("Add points");
        }
    }
}

Other thing you talk about accessing score counter from other script.You can achieve this by using many techniques as by taking reference of the class or declaring static counter etc
We have a class of ScoreSystem

using UnityEngine;
public class ScoreSystem : MonoBehaviour
{
    public static int Score;
}

you have other class triggerScript then you can change Score in your triggerScript class just by calling class name.

using UnityEngine;
public class triggerScript : MonoBehaviour
{
    private void Start()
    {
        ScoreSystem.Score++;
    }
}