pickup-script help

Tried to search for help write/edit a script for pickup. I have 3 different items with different points, but when i’m trying collecting one item the points for every item gets added on the score for each, so 1 item = 6 points. I see that i probably must add some if else statements, but when i try i either looses sound (it’s a different sound for each item,), have the same problem or it’s not working at all. Can i please get some input on how to manage this?

using UnityEngine;
using System.Collections;

public class pickup : MonoBehaviour {


public int PointOneValue;
public int PointTwoValue;
public int PointThreeValue;

private bool _triggered;
private AudioSource _lydkilde;


	// Use this for initialization
	void Awake () 
	{
		PointOneValue = 1;
		PointTwoValue = 2;
		PointThreeValue = 3;
		_lydkilde = gameObject.GetComponent<AudioSource>();
	}
	
	// Update is called once per frame
	void Update () {
		if (_triggered  !_lydkilde.isPlaying)
			Destroy(gameObject);
	}
	
	// On sound the score get's updated.
	void OnTriggerEnter()
	{
		_triggered = true;
		
		_lydkilde.enabled = true;
		
		Spillet.PickUpCount += PointOneValue;
		Spillet.PickUpCount += PointTwoValue;
		Spillet.PickUpCount += PointThreeValue;

		
	}
}

What you are doing is that when you have collided with your object, you are adding all 3 different points to your total score. You’ll want to do a check in your OnTriggerEnter function, probably by checking the tags of the object.

void OnTriggerEnter(Collider other)
{
if(other.tag == "point1")
{
// Add one point and play sound
}
else if(other.tag == "point2")
{
// Add two points and play sound
}
else if(other.tag == "point3")
{
// Add three points and play sound
}
}

It might make more sense to put the score value on the object you’re picking up… attach a script to the pickup objects with a “public int scoreValue;” line; doesn’t need any functions. You can then set the score values in the inspector (or when they’re instantiated if they’re procedurally generated).

Then in your collision function you can use:

 other.gameObject.GetComponent(whateveryoucalledthescript).scoreValue

to access that value and add it to your pickupcount. You can then set any value, or have more pickups without having to rewrite your code.

Thank you, that worked, and so easy :smile: