Buff System

I want to make a buff system so when i walk over a object it buffs me, a bit like a health pack thing, where you walk over a health pack and you gain health but for a var called weaponStrenth.
im new to coding and really want to become good at it,but i cant find any help on this anywere.
All help is usefull :slight_smile: Thanks.

I don’t know what you mean by “weaponStrength”, but I will give you a code anyways.

This is going to be a huge answer, but helpfull for someone just started coding, with extra explanation and stuff.

Well, for this we use a collider so:

-Add collider(mesh, box, or whatever you want to use, no rigidbody!)

-Set the collider to “isTrigger”, that way you don’t bounce off the object

-Create a tag, and call it “Pickup” for now, you can always change it

-Add the following code to your player

** I will give you a code to apply to the player,and I will give you a code wich you can use to apply to the pickup object, you will have to choose 1 AND(yes there’s more!) I will give you another script wich you can use in combination with the script following and it’s called pickupscript for now! More info after the main code!

-So, add this code:

var weaponStrenght : int;

function OnTriggerEnter(hit : Collider //this is to define wich object you hit!){

var pickupscript = hit.GetComponent(pickupscript);

if(hit.transform.tag == "Pickup"){
   pickupscript.BuffAmount += BuffAmount;//I'm calling a static value in the pickup script
   //pickup script will come after this
}
}

A little extra explanation: I’m using the OnTriggerEnter function wich will check if you have entered a collider that is a trigger(note that we just set the collider of the pickup as a trigger!).

I used hit.GetComponent(pickupscript), the hit is for the collider we hit, in the object we hit, we check if it has a component called pickupscript(we will create this soon!)

We use that script to acces a variable, actually it’s a static variable(so we can acces this very easy, lolz I just found out that you don’t need the component, but it could be more easy and a good example to use later!)

Now…it’s time…THE PICKUPSCRIPT!!!

static var BuffAmount : int = 10;
//and you could add some extra variables!

This is all you need! BTW: I’m not sure if it needs to be static, check plz if it has to be static you can’t acces it from the inspector, but there’s a workaround and here it is!:

static var BuffAmount : int = 10;
var BuffAmount_holder : int = 10;

function Start(){
BuffAmount = weaponStrenght_holder;
}

Add the main script to the player and the pickup script to the pickup.
You don’t need the pickup script if you will not use it, you will have to declare the variables in the main script, if you have alot of pickup types it’s easy to use 1 script to add to those pickups, or if you want big and small pickups this is easy!

Now to only use the script with the pickup, so to turn it around:

var BuffAmount;//and other buff types, I'm not going to fully declare it


function OnTriggerEnter(hit : Collider //this is to define wich object you hit!){

var playerscript = hit.GetComponent(playerscript);

if(hit.transform.tag == "Player"){
   playerscript.weaponStrenght += BuffAmount;
}
}

Sorry for the crazy long answer…this is the longest answer I ever did(I felt helpfull, and I just needed to…SCRIPT ALL THE SCRIPTS!!!).
If you give me some specific info, I will see what I can add, edit and how I can help you!

Good luck,
-Hybris