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 ThomasMarsh · Nov 11, 2012 at 08:59 PM · c#collisionvariabledamage

Editing a variable from another script on collision

My goal is to be able to edit the variable(Current Health) from the first script when my projectile collides with an object containing AIHealth(script 1) from a second script. AI Skelly Prefab contains the AIHealth script. The second script cannot seem to find the variable in the other script.

First Script(AIHealth.cs):

 using UnityEngine;
 using System.Collections;
 public class AIHealth : MonoBehaviour {
     public float MaxHealth=100;
     public float CurrentHealth;
     public bool Dead;
     
     void Awake () {
     CurrentHealth=MaxHealth;
     }
     void Update() {
     
     if(CurrentHealth<=0){
             CurrentHealth=0;
             Dead=true;
         }
     
     if(CurrentHealth>=MaxHealth)CurrentHealth=MaxHealth;
     
     if(Dead){
     
     }
     }
 }


Second Script(Damage.cs):

 using UnityEngine;
 using System.Collections;
 
 public class Damage : MonoBehaviour {
     public float damage=60;
     public static int CurrentHealth = 100;
 
     
 void OnCollisionEnter(Collision other){
           if(other.gameObject.tag == "AI Skelly Prefab"){
                 var target = other.gameObject
                 var ch = target.transform.GetComponent("AIHealth").CurrentHealth;
                     ch -= damage;
   }
 }
 
         
 }
Comment
Add comment · Show 1
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 ThomasMarsh · Nov 11, 2012 at 09:31 PM 0
Share

If this is not possible, is there a different way to do it that accomplishes the same task?

3 Replies

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

Answer by sparkzbarca · Nov 12, 2012 at 06:12 AM

you declared a bool known as dead, you did not set it to false and bools by default are true I believe, so thats actually a bit odd as the object should in fact insta die. :P

the gameobject in this case is what? what exactly did you type for it?

Im wondering if you just ended up trying to destroy the script or something.

For example is you used the keyword this.

so Destroy(this); That would remove the script.

you need to make sure you pass it the actual AIEnemy

Lastly i dont think that ch variable thing is working, honestly the problem is with unity I'm not sure if unity is using pointers or not but I think in that case its not.

I think whats happening here is

lets say the aiscript has current health and its equal to 100;

so

 aihealth = 100;
 ch = get aihealth;
 so 
 ch = 100;
 
 now
 ch = ch - 20;
 
 so 
 ch = 80
 
 BUT
 aihealth still equals 100
 
 thats because ch isn't aihealth. its a copy of aihealth.
 
 what you need to do is get rid of what is honestly an unneeded variable anyways
 
 just do 
 
 target.transform.GetComponent<AIHealth>().CurrentHealth -= damage;
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 Seth-Bergman · Nov 12, 2012 at 07:02 AM 2
Share

well, bools will default to false, actually.. but otherwise dead on.

It should say:

 void OnCollisionEnter(Collision other){
      if(other.gameObject.tag == "AI Skelly Prefab")
 other.gameObject.GetComponent<AIHealth>().CurrentHealth -= damage;
 }
avatar image ThomasMarsh · Nov 12, 2012 at 05:42 PM 0
Share

I changed the things you suggested and tested the game but no matter what I shot, it still does not do damage. I checked and it has nothing to do with the death.

avatar image ThomasMarsh · Nov 13, 2012 at 02:49 AM 0
Share

I have done some tests, and it seems like the problem is in

void OnCollisionEnter(Collision other){ if(other.gameObject.tag == "Enemy"){ other.gameObject.GetComponent().CurrentHealth -= damage; Debug.Log("damage"); } }

} The debug.log is not returning anything when I hit the enemy. Could it be a problem with the if statement?

avatar image ThomasMarsh · Nov 13, 2012 at 03:58 AM 0
Share

I finally fixed it by messing with the gameObject tag. Thank you for all the help.

avatar image
0

Answer by sparkzbarca · Nov 11, 2012 at 10:36 PM

you can't use the string version of getcomponent.

All you need to do is change this line to a type. dont forget the () after the type.

 target.transform.GetComponent<AIHealth>().CurrentHealth;


see this

http://unitygems.com/mistakes1/

for that and other common mistakes

Comment
Add comment · Show 2 · 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 ThomasMarsh · Nov 12, 2012 at 02:13 AM 0
Share

I changed that line of code and did some reading of the guide, which is very helpful, thank you for pointing me to it, and when I build the script it showed no errors. I also changed the if(Dead){ to include Destroy(gameObject); however, when I test the game, the enemy is not destroyed no matter the number of arrows.

avatar image sparkzbarca · Nov 12, 2012 at 06:09 AM 0
Share

you declared a bool known as dead, you did not set it to false and bools by default are true I believe, so thats actually a bit odd as the object should in fact insta die. :P

the gameobject in this case is what? what exactly did you type for it?

Im wondering if you just ended up trying to destroy the script or something.

For example is you used the keyword this.

so Destroy(this); That would remove the script.

you need to make sure you pass it the actual AIEnemy

Lastly i dont think that ch variable thing is working, honestly the problem is with unity i'm not sure.

I'm not sure if unity is using pointers or not but I think in that case its not.

I think whats happening here is

lets say the aiscript has current health and its equal to 100;

so

aihealth = 100; ch = get aihealth; so ch = 100;

but thats a copy

avatar image
0

Answer by CostelloNicho · Nov 12, 2012 at 06:21 AM

Sounds like you should really be using a c# event. If I were you I would set up an entire event messaging system. In this way you can handle Broadcasted events within each script subscribed without outside scripts manipulating your variables (generally frowned upon). Here's a link to the c# messenger which I believe follows standard design paradigms:

c# advanced messaging 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

12 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

Related Questions

Using a script as a member variable in another script. 2 Answers

How Can One Collider Recognize Contact With Another? 0 Answers

Distribute terrain in zones 3 Answers

Check different collisions 1 Answer

Javascript not being updated, variables being overridden, but C# is fine 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