Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GameManiac · Feb 19, 2014 at 03:47 AM · if-statementsconditionkillkill player

how can i have an if statement to kill a player if they dont have an item

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;
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by J-R-Wood · Feb 19, 2014 at 04:51 AM

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):
 }
 
 
 
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image J-R-Wood · Feb 19, 2014 at 04:54 AM 0
Share

If that doesn't work or your getting errors just let me know here, or my email which is josiahrwood@gmail.com

avatar image GameManiac · Feb 19, 2014 at 05:22 AM 0
Share

Hmm before i try this i played with my scripts and i created one that looks for a specific tag that you run into before you enter the killzone and it wouldn't work when i tagged the candle which was parented to the player so it was its child however when i made the tag it noticed the player itself it worked so i am guessing that the reason that it doesn't work is because it doesn't look for the child and only the parent would you happen to know how to look for the child tag ins$$anonymous$$d of the tag?

Here is that script:

 var kill:GameObject;
         
 function OnTriggerEnter (other : Collider) {
          
  if(other.tag == "Candle"){
   Destroy(kill);
  }
 }
avatar image J-R-Wood · Feb 20, 2014 at 12:34 AM 0
Share

I don't sorry

avatar image
0

Answer by brent ragsdale · Feb 19, 2014 at 06:45 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object will not instantiate 1 Answer

Question on Possible FOR Loop Condition Conflict 0 Answers

Create a if statement using GetComponent? 1 Answer

conditional spawning 1 Answer

Killer + assistance id for shooter game 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges