how can i have a conditional to kill a player if they dont have an item. For instance, in my game i have a kill volume whenever the player tagged as player approaches it but i want them not to die if they have a particular item. the item i tagged as candle is what i want them to have in their inventory or parented to them so they don’t die. i am new to unity so i have no idea what to do. Please help
Here is what i have:
var RespawnPosition:Transform;
function OnTriggerEnter(other: Collider){
if (other.tag == "Candle"){
}else (other.tag == "Player"){
other.transform.position = RespawnPosition.position;
other.transform.rotation = RespawnPosition.rotation;
}
}
Create a Collider for the candle and click the box that says trigger then the script you have to kill the player add to a empty game object then Create a Collider and click the box that says trigger on the empty game object make sure the trigger on candle is larger then the trigger or colliders on you player
then add this script to your empty game object that holds the kill player script
var script : ScriptName;
function Update ()
{
script = GetComponent("ScriptName");
}
OnTriggerEnter (col : Collsision)
{
if (col.gameObject.tag=="candle")
script.SetActive(false):
}
OnTriggerExit (col : Collsision)
{
if (col.gameObject.tag=="candle")
script.SetActive(true):
}
I wouldn’t bother with colliders or parenting (Unless the candle is already a child object).
For Collision:
function OnTriggerEnter(other : Collider) {
//Check to see if it's a player who triggered the event. Make sure your player object is tagged as "player". (No quotes)
if (other.tag == "player") {
//Okay, it's our player. Let's access his Inventory script and see if he has a candle.
if(!other.gameObject.GetComponent("Inventory").HasCandle()) { //Note! You do not include the extension when calling GetComponent. Inventory.js becomes Inventory.
//Uh-oh, looks like our player object doesn't have a candle, destroy him!
Destroy(other.gameObject);
}
}
}
Ok, now for the player code. If you handle your player inventory with a script not attached to the player, then you’ll have to change the above line “!other.gameObject.GetComponent(“Inventory”).HasCandle()” to “!GameObject.Find(“where-ever-your-inventory-script-is-attached”).GetComponent(“Inventory-script-here”).HasCandle()”. Don’t forget the leading “!”, that’s important.
Otherwise, continue as normal:
//Player Inventory Script, named Inventory.js.
var candle : Boolean;
//This function's code should be placed in your regular pick up item function, however you choose to handle that.
function PickUpItem() {
if (itemPickedUp.tag = "candle") {
candle = true;
}
}
//This function's code should be placed in your regular drop/destroy/used-up item function, however you choose to handle that.
function DestroyItem() {
if (itemDestroyed.tag = "candle") {
candle = false;
}
}
//This function should be copied completely to your Inventory script.
function HasCandle() {
return candle;
}
EDIT: Of course you’ll need to adjust this last section of code to account for a play being able to pick up more than one candle. That would easily be solved by running a check of Inventory.AmountOfItem(123), where 123 is the array id of your candle gameObject. You could then replace HasCandle with a simple check of
if(other.GetComponent("Inventory").AmountOfItem(123) <= 0){
Destroy(other.gameObject);
}
You would need to code a function for AmountOfItem(x) to fit your inventory system.