Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by cbutton · Apr 23, 2017 at 07:12 PM · 2djavascriptsystemartificial intelligenceneed

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 () {
     
 }
Comment
Add comment · Show 4
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 Glurth · Apr 23, 2017 at 07:59 PM 0
Share

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);


avatar image cbutton Glurth · Apr 23, 2017 at 08:07 PM 0
Share

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

avatar image Glurth cbutton · Apr 23, 2017 at 08:15 PM 0
Share

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?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 4 · 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 cbutton · Apr 24, 2017 at 09:02 PM 0
Share

So if I'm adding animations later, it would be easier to use the animation with ontrigger enter correct?

avatar image sadowlight123 cbutton · Apr 25, 2017 at 09:05 PM 0
Share

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

avatar image Glurth · Apr 24, 2017 at 10:28 PM 0
Share

$$anonymous$$akes a few assumptions about WHEN to eat the rat (which I would recommend you explain), but definitely a good answer, +1

avatar image sadowlight123 Glurth · Apr 25, 2017 at 09:06 PM 0
Share

Thank you for the advice and support Glurth

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

178 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 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 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 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 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 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 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 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 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

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

Unity2d Error BCE0043: Unexpected token 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