# Coin script help

**URL:** https://discussions.unity.com/t/coin-script-help/482539
**Category:** Scripting
**Created:** [July 31, 2012, 4:01am UTC](https://discussions.unity.com/t/coin-script-help/482539 "2012-07-31T04:01:12Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![zAk11](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zAk11](https://discussions.unity.com/u/zAk11)
#### Post date: [July 31, 2012, 4:01am UTC](https://discussions.unity.com/t/coin-script-help/482539/1 "2012-07-31T04:01:12Z")

</div>

Hello, I’m trying to make a coin in my game. So far I’ve been able to collect the coin (player touches coin, coin disappears) but the GUI doesn’t register that I’ve collected a coin. I looked around in the scripts and I can’t seem to find the error. Any help would be greatly appreciated.

Thanks,  
zAk

the Pickup script (attached to coin)

```auto
enum PickupType { Health = 0, FuelCell = 1 }

var amount = 1;
var sound : AudioClip;
var soundVolume : float = 2.0;
var CoincounterComponent: Component;
var pickupType = PickupType.Coin;
private var used = false;

function Start ()
{
	// do we exist in the level or are we instantiated by an enemy dying?
	mover = GetComponent(DroppableMover);
}

function ApplyPickup (playerStatus : ThirdPersonStatus)
{

CoincounterComponent = GetComponent("Coincounter");
	switch (pickupType)	
	{	
	
		case PickupType.Coin:
			playerStatus.FoundItem(amount);
			break;	
	// A switch...case statement may seem overkill for this, but it makes adding new pickup types trivial.
	
	case PickupType.Coin:
			playerStatus.FoundItem(amount);
			break;	

		
	}
	
	return true;
}

function OnTriggerEnter (col : Collider) {
	if(DroppableMover DroppableMover.nabled) return;
	var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus);
	
	//* Make sure we are running into a player
	//* prevent picking up the trigger twice, because destruction
	// might be delayed until the animation has finished
	if (used || playerStatus == null)
		return;
	
	if (!ApplyPickup (playerStatus))
		return;

	used = true;
	
	// Play sound
	if (sound)
		AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);
		
	
	
	// If there is an animation attached.
	// Play it.
	if (animation animation.clip)
	{
		animation.Play();
		Destroy(gameObject, animation.clip.length);
	}
	else
	{
		Destroy(gameObject);
	}
}

// Auto setup the pickup
function Reset ()
{
	if (collider == null)	
		gameObject.AddComponent(BoxCollider);
	collider.isTrigger = true;
}

@script RequireComponent(SphereCollider)
@script AddComponentMenu("Third Person Props/Pickup")

```

The Coincounter script (attached to camera)

```auto
static var pointCoin : int = 0;

 

function Start ( ) {

 

         GameHUDComponent = GetComponent(GameHUD);

 

}

 

function OnGUI( ) {

 

          GUI.Label (Rect (10, 10, 100, 20), "Coins: " + pointCoin);

 

}

```

---

<div class="post-metadata">

### Author: ![Poq-Xert](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@Poq-Xert](https://discussions.unity.com/u/Poq-Xert)
#### Post date: [July 31, 2012, 5:21am UTC](https://discussions.unity.com/t/coin-script-help/482539/2 "2012-07-31T05:21:49Z")

</div>

pointCoin variable is always zero, it never changes …

---

<div class="post-metadata">

### Author: ![zAk11](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zAk11](https://discussions.unity.com/u/zAk11)
#### Post date: [July 31, 2012, 1:34pm UTC](https://discussions.unity.com/t/coin-script-help/482539/3 "2012-07-31T13:34:05Z")

</div>

So the error is in the Coincounter script?

---

<div class="post-metadata">

### Author: ![arkon](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/arkon/32/537676_2.png) [@arkon](https://discussions.unity.com/u/arkon)
#### Post date: [July 31, 2012, 2:37pm UTC](https://discussions.unity.com/t/coin-script-help/482539/4 "2012-07-31T14:37:11Z")

</div>

its in both scripts. You never increment pointCoin anywhere. you need to increment it when you touch the coin.

---

<div class="post-metadata">

### Author: ![zAk11](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zAk11](https://discussions.unity.com/u/zAk11)
#### Post date: [July 31, 2012, 3:08pm UTC](https://discussions.unity.com/t/coin-script-help/482539/5 "2012-07-31T15:08:28Z")

</div>

How do I do that? I can’t find Pointcoin in both scripts. Or is that the problem, that Pointcoin is not in both scripts?

---

<div class="post-metadata">

### Author: ![pat\_sommer](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/pat_sommer/32/276413_2.png) [@pat\_sommer](https://discussions.unity.com/u/pat_sommer)
#### Post date: [July 31, 2012, 3:13pm UTC](https://discussions.unity.com/t/coin-script-help/482539/6 "2012-07-31T15:13:35Z")

</div>

inside the ontriggerenter add in

Coincounter.pointCoin +=1;

---

<div class="post-metadata">

### Author: ![zAk11](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zAk11](https://discussions.unity.com/u/zAk11)
#### Post date: [July 31, 2012, 3:38pm UTC](https://discussions.unity.com/t/coin-script-help/482539/7 "2012-07-31T15:38:42Z")

</div>

Thanks, it worked!

---

<div class="post-metadata">

### Author: ![arkon](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/arkon/32/537676_2.png) [@arkon](https://discussions.unity.com/u/arkon)
#### Post date: [August 1, 2012, 12:16am UTC](https://discussions.unity.com/t/coin-script-help/482539/8 "2012-08-01T00:16:00Z")

</div>

pointCoin was in your OnGUI function and this was the value you were trying to print to the screen. pat’s answer will work but I’m worried why you couldn’t find the variable. Make sure you are not doing case sensitive searches when looking for things.

---

<div class="post-metadata">

### Author: ![pat\_sommer](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/pat_sommer/32/276413_2.png) [@pat\_sommer](https://discussions.unity.com/u/pat_sommer)
#### Post date: [August 1, 2012, 12:24am UTC](https://discussions.unity.com/t/coin-script-help/482539/9 "2012-08-01T00:24:17Z")

</div>

he couldnt find the component because both scripts are not attached to the same object, when using the get component he has to tell the pick up script to talk to the the coin script on the main camera, he never defines this, also never added to the coin number

if you want to use get component thats fine, but you dont need your variable to be static in that case, so your get component is basically not doing anything to talk to the variable

when you use static it lets other scripts talk to it directly wherever it is, this can get messy but can be helpful in some cases.

for example say you have 20 characters in a scene and you want to adjust the speed of just one of them, yet your setting speed as a static var, when you adjust speed all 20 characters will start moving fast, this is the case where get component is needed

---

<div class="post-metadata">

### Author: ![ankitkcapp11](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@ankitkcapp11](https://discussions.unity.com/u/ankitkcapp11)
#### Post date: [July 3, 2020, 1:01pm UTC](https://discussions.unity.com/t/coin-script-help/482539/10 "2020-07-03T13:01:51Z")

</div>

Please someone help  
Coin is not showing up in the screen…

Also there is an error on console when the player aj collides with the coin:

NullReferenceException: Object reference not set to an instance of an object  
gb.OnTriggerEnter (UnityEngine.Collider

CODE:  
{  
// Start is called before the first frame update  
public GameObject gb\_score;  
public float coin\_amt = 0;  
// Start is called before the first frame update  
void Start()  
{

}  
void OnTriggerEnter(Collider col)  
{

if (col.name == “aj”)  
{  
coin\_amt += 1;  
gb\_score.GetComponent().text = coin\_amt.ToString();  
Destroy(gameObject);  
}  
}
