grab variable from an instance of a game object?

Hello all!
i have a ball that rolls around and i want it to collect coins (that come in 3 different value types based on their prefabs). i know the long way to hard code it out by setting unique tags to every coin prefab and note their values(5, 10, and 25 respectively) without having to access the outside script but obviously that’s not very efficient however still viable if need be.

so i have a CoinScript that holds the rotation of the coin and its value that looks like this:

using UnityEngine;
using System.Collections;

public class CoinScript : MonoBehaviour {
	
	public int axis1;
	public int axis2;
	public int axis3;
        // rotations, not pertinent to this question

	public int coinValue;     // the value i want to access from player stats


	// Update is called once per frame
	void Update () {
		transform.Rotate (new Vector3 (axis1, axis2, axis3) * Time.deltaTime );

		

	}
}

I set coin value to a public variable so i can use a single script and set the value per coin prefab.
(again to 5, 10 , and 25 for bronze, silver, and gold coins)
the script that im trying to use to access the value looks like this:

using UnityEngine;
using System.Collections;

public class PlayerStats : MonoBehaviour {
	// player counts
	public int coinCount;
	public int gemCount;
	//values for these pickups
	//public int coin1Value;
	//public int coin2Value;
	//public int coin3Value; ignore these (they are for the hard coding option)
	private GameObject target;


	// Use this for initialization
	void Start () {
		target = GetComponent<CoinScript> ();
	}
	void OnTriggerEnter(Collider other){
	
		if (other.gameObject.CompareTag("Coin")) {
			other.gameObject.SetActive (false);

			coinCount += target.coinValue; //this accesses the variable from 
			//coinscript that changes by coin variation
                    //i get a null reference error when running (and the attribute coinValue DOES appear  in the quick select menu when typing in target."..."
		}
	}
}

i am aware that i set the target game object to private, but i figured (i probably should have tested a lot before posing this question) that if i set the target to every instance of a coin(the game object that will become a prefab and spread throughout a level), i would run into problems of trying to access coin values as ill have many prefabs active in a scene at the same time with different values.

would this work if i do set the trigger to be to the coin(s)?

as there are only 3 different values of coins in play here, it would not be the end of the word to have to create a few more tags for different values.

is hard coding the best way to go in this case or is there still yet another method i am overlooking to run this properly?

thanks!

The variable coinValue is a member of a CoinScript component, not of a GameObject. Hence, you have to use GetComponent to access the script that is attached to the GameObejct.

void OnTriggerEnter(Collider other){
     
         if (other.gameObject.CompareTag("Coin")) {
             other.gameObject.SetActive (false);
 
             coinCount +=  other.gameObject.GetComponent<CoinScript>().coinValue; //this accesses the variable from 
             //coinscript that changes by coin variation

         }
     }

This assumes that every coin has a CoinScript component. This eliminates the need for the “target” variable. If you want to access the CoinScript components on your coin objects, then it’d be a good idea to keep a collection of these scripts, like Generic List, where you add and remove elements each time you instantiate or destroy a coin.