Coin script help

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)

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)

static var pointCoin : int = 0;

 

function Start ( ) {

 

         GameHUDComponent = GetComponent(GameHUD);

 

}

 

function OnGUI( ) {

 

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

 

}

pointCoin variable is always zero, it never changes …

So the error is in the Coincounter script?

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

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?

inside the ontriggerenter add in

Coincounter.pointCoin +=1;

Thanks, it worked!

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.

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

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);
}
}