- Home /
Help needed with a pickup script
ok i am trying to make a script where you pick up materials which you use later in the game to make other things but whenever the main character picks up an item on the collision list it adds to all of the materials instead of the one assigned... i feel like its really obvious what i did wrong but i cant figure out what
var addmat1 = 5;
var mat1 = 0;
var addmat2 = 1;
var mat2 = 0;
var addmat3 = 2;
var mat3 = 0;
var addmat4 = 2;
var mat4 = 0;
var collide1 : GameObject;
var collide2 : GameObject;
var collide3 : GameObject;
var collide4 : GameObject;
function OnTriggerEnter (Collision: Collider) {
if (Collider.GameObject == collide1);
mat1 += addmat1;
if (Collider.GameObject == collide2);
mat2 += addmat2;
if (Collider.GameObject == collide3);
mat3 += addmat3;
if (Collider.GameObject == collide4);
mat4 += addmat4;
}
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), "Nails:" +mat1);
GUI.Label (Rect (60, 10, 100, 20), "Wood:" +mat2);
GUI.Label (Rect (110, 10, 100, 20), "Metal:" +mat3);
GUI.Label (Rect (160, 10, 100, 20), "Misc:" +mat4);
}
Answer by save · Apr 01, 2012 at 10:53 PM
You need to set the type of your variables, it isn't crucial to the problem but important for the future.
var addmat1 : int = 5;
You're not using the variable returned from the collision.
function OnTriggerEnter (Collision: Collider)
//Should be
function OnTriggerEnter (other : Collider)
//As this is the data from the other colliding object, not the collision
//Then use
if (other.GameObject == collide1) {
//As garner stated do not end the if-statements with semicolon
}
You might as well use the colliders as variables instead of calling the GameObjects if you don't plan on using the reference later.
var collide1 : Collider;
if (other == collide1) {}
I recommend you to start using #pragma strict in the top of your scripting documents to disable dynamic scripting, you will have a much easier pipeline for errors, and the main reason, much faster code.
Although when creating pickups it's usually better to let the pickups have values that passes on to a handler. So if you'd create a pickup-script and attached it to the GameObject, then you'll be able to give it different amounts of values, for instance a pile of gold is more worth than a single gold coin but increases the same value.
You could start off with an enum setting the type of pickup, then give that particular pickup a value.
enum PICKUP {
Nails,
Wood,
Metal,
Misc
}
//From the inspector you can switch this and make the object into a prefab
var pickupType : PICKUP = PICKUP.Nails;
var amount : int = 1;
Now when the collision occurs you can ask the colliding object what it is and how much it is worth.
function OnTriggerEnter (other : Collider) {
if (!other.CompareTag("Pickup")) return; //Make sure that this is a pickup else exit (needs a tag set in the Inspector)
var pickupScript : PickupScript = other.GetComponent(PickupScript); //Get the script from the colliding object
DoSomething(pickupScript.pickupType, pickupScript.amount); //Send the type and amount into a function
}
//Take care of the amount that should get stored into corresponding variable
function DoSomething (type : PICKUP, amount : int) {
switch (type) {
case PICKUP.Nails: mat1+=amount; break;
case PICKUP.Wood: mat2+=amount; break;
case PICKUP.Metal: mat3+=amount; break;
case PICKUP.Misc: mat4+=amount; break;
}
}
Answer by garner · Apr 01, 2012 at 10:00 PM
you've ended the if statements with semicolons they shouldn't have semicolons this is why it's assigning everything regardless. Also try Collider.GameObject.name and using 'else if' is useful here.