Destroy object with trigger and add value to variable
So the player character and the npcs in this game have the same need system. For example hunger is what I am working on right now. I know how to make the number drain to zero (or more) but what I need to do next is destroy a game object (eat it) and increase the value by a certain amount. The player needs to press e near an object, but NPCs should be able to do it themselves
#pragma strict
public var fullness = 100;
public var fatigue = 100;
public var healthPoints = 10;
public var drainRate = 5;
function eat(){
if(Input.GetKey("e")){
fullness = fullness + 5;
Destroy(rat);
}
}
function hunger() {
fullness = fullness - drainRate;
}
function Start () {
InvokeRepeating("hunger",.01, 5.0);
}
function Update () {
}
hmm, I don't see where you declare the variable that you're destroying "rat". These two lines seem like they should achieve the objective in the title: what goes wrong?
fullness = fullness + 5;
Destroy(rat);
unity doesn't recognize identifier rat, when I press e the fullness goes up by 100. I fixed that so now it maxes out at 100 and wont go higher and no more than 5 at a time. I just don't know how to make it conditional upon destroying the game object
The compiler error means you need to tell it that a variable named rat exists, and what kind of object it is: this is called "declaring a variable". Note: you will need to BOTH declare the variable AND assign a value to it before you can attempt to destroy it. So I'd first ask, how do you know WHICH object in the scene you want to be eaten and destoryed?
Answer by sadowlight123 · Apr 24, 2017 at 08:29 PM
Hello there , I am new to trying to answer questions :) If I understood well , you want to destroy the game object called rat ... this cannot be done unless you "declare" rat. for example you declare an int x or a string y , in this case you need to declare a gameobject rat then you can type destroy(rat).
however for the purpose you are trying to reach i think it might be better to write the following:
function OnTriggerEnter (other : Collider)
{
if(other.tag=="rat")
{
// remove eat function
if(Input.GetKey("e")){
fullness = fullness + 5;
Destroy(other.gameObject);
}
}
and don't forget to add a tag rat to your rat object in the scene and add a collider and check the trigger box
So if I'm adding animations later, it would be easier to use the animation with ontrigger enter correct?
it should . you just add the additional code be it play sound effect , play animation there under if(Input.Get$$anonymous$$ey("e")){}
sometimes if you want to do animations and you have a bool , you would rather set the bool true or false there in the OnTriggerEnter but you put the anim.setbool() under function update
$$anonymous$$akes a few assumptions about WHEN to eat the rat (which I would recommend you explain), but definitely a good answer, +1
Your answer
Follow this Question
Related Questions
Destroying a projectile on collision 1 Answer
Instantiate Script Not Working 1 Answer
Need your IDEAS!! (Players building their own base) 2 Answers
Basic AI avoidance in 2D 1 Answer