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 Jason991 · Dec 12, 2013 at 03:19 AM · c#enemyenemyai

How to make if the object == null then add gold?

Hi guys, okay my problem goes like this i have a damage script for the enemy in javascript then my add gold script is in C#, right now i wanted to make a C# script that able to state if the enemy is dead via gameObject == null then will Player.gold + 1. Is it possible guys? A script that will detect if the object is destroy or no longer there then auto add gold rather than when HP gone only add gold. The below script is not working, anyone have idea?

Enemy.cs public class enemy : MonoBehaviour {

     public GameObject gameObject;
     
 
     // Use this for initialization
     void Start () {
         
         gameObject = GameObject.FindWithTag("Player1");
     
     }
     
     // Update is called once per frame
     void Update () {
         
         if (gameObject == null){
             
             Players.gold = Players.gold + 1;
             
             Destroy (gameObject);
         }
     
     }
 }
 
Comment
Add comment · Show 8
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 tw1st3d · Dec 12, 2013 at 03:21 AM 0
Share

First thing I see is that you're using gameObject for your target. gameObject is a direct reference to Unity's gameObject internal functions and properties, directly affecting the object that the script is attached to. I believe that using it as a variable name will throw errors.

avatar image Jason991 · Dec 12, 2013 at 04:26 AM 0
Share

hmmm sir due to my limited knowledge i don't quite get you, if for my case how should i define it?

avatar image robertbu · Dec 12, 2013 at 05:09 AM 0
Share

Just change the name of the variable to something other than 'gameObject.' Use 'target', or 'go', or 'player', or...

Also your logic will cause an error. I'm not sure, but I'm guessing you want != ins$$anonymous$$d of ==. The code as it stands will attempt to destroy a 'null' game object resulting in an error.

avatar image united4life · Dec 12, 2013 at 05:25 AM 0
Share

What he meant is you should give some other name for gameObject, like this GameObject target

You have written your if condition in update so it will keep on adding the gold.

Write somewhere else where the method tuns only once or take a bool variable and run it once

avatar image ikelaiah · Dec 12, 2013 at 05:44 AM 0
Share

Furthermore, what he meant is you should give variable names that are relevant to what the script is doing. $$anonymous$$gestion by @robertbu sounds good if you want to add gold everytime Update() is called. Otherwise, do suggestion by @united4life.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Frank126 · Dec 26, 2013 at 07:53 PM

 OnDestroy(){
     Players.gold += 1;
 }    

Whenever the GameObject is destroy your player will receive the amount of gold set.

For more info about Unity Monobehavior event see the "Messages" section at this Link

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

[I REALLY NEED HELP FAST]Help with enemy Shooting 1 Answer

Using Vector3.distance without the y axis 5 Answers

2D AI, Aim at player - even when jumping? 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