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 Persona · Apr 02, 2011 at 02:38 PM · collisionvariablestaticprivatehealth

Specific enemy variable affects all enemies

All the enemies in my game have health and on collision with the player, they subtract their current level of health from the players.

The problem is that if the player reduces one of their health to zero, all of them disappear. At the same time, if I set their health to private variable, the health won't sutbract from the enemies health on collision.

public var Health: int = 100;

function OnCollisionEnter(other: Collision){ if(other.gameObject.CompareTag("Player")){ other.gameObject.Health -= Health; Destroy(gameObject); rigidbody.velocity = -rigidbody.velocity; }

Comment
Add comment · Show 3
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 AngryOldMan · Apr 02, 2011 at 03:00 PM 0
Share

try using a public variable, we had similar problems and ours is now a public variable, however I did do a few other things which I can't recall (chopping and changing solutions got all mixed up) so it may or may not work. :(

avatar image Persona · Apr 02, 2011 at 06:26 PM 0
Share

That didn't work. It could call the Health from the other scripts.

avatar image flaviusxvii · Apr 05, 2011 at 04:47 PM 0
Share

This question is really confusing, almost every use if 'their' or 'health' is ambiguous.

You're reducing player health by enemy health each time an enemy collides with a player? The code looks straightforward. I don't understand your problem.

2 Replies

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

Answer by karl_ · Apr 05, 2011 at 04:21 PM

I'm not sure if this is exactly what you're looking for, but it should at least give you an idea as to how to go about modifying variables in other scripts without using static vars. If this script is attached to an enemy object any time it comes into contact with the player it will subtract it's current health from the player then self destruct.

http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html

var enemyHealth : int;

function OnCollisionEnter(col : Collision) { if(col.gameObject.CompareTag("Player")) { var tempScript = col.gameObject.GetComponent("name of script that handles health"); tempScript.health -= enemyHealth; Destroy(gameObject); } }

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 Persona · Apr 05, 2011 at 05:22 PM 0
Share

Small error: var tempScript = col.GetComponent("name of script that handles health");

Is suppose to be: var tempScript = col.gameObject.GetComponent("name of script that handles health");

avatar image karl_ · Apr 05, 2011 at 08:01 PM 0
Share

Ah, true. I'll update the answer with this inclusion.

avatar image
0

Answer by Justin Warner · Apr 02, 2011 at 03:01 PM

In Java, their are things called getter/setter methods, which are private, to access private variables...

So, try making it private, and then add in a setter method, that brings in the damage, and then subtract it and set it there.

Make sure it's not static too...

Um, can't think of anything else right now, but you can try this =).

Comment
Add comment · Show 8 · 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 Owen-Reynolds · Apr 02, 2011 at 05:56 PM 0
Share

It's gotta be that enemy Health is a static.

avatar image Persona · Apr 02, 2011 at 06:30 PM 0
Share

But if it's not static, then other game objects can't interact with it.

Justin, I need for you to elaborate a bit more on it.

avatar image Owen-Reynolds · Apr 03, 2011 at 05:14 PM 0
Share

Static means there is one copy for ALL enemies. Public/private means "others can/can't see/change it."

avatar image Justin Warner · Apr 03, 2011 at 07:39 PM 0
Share

Owen, I think you're thinking of it wrong... Static is the same for all people... So, if you have a static health, you kill one person, and it kills all of them... You need to just make it private, and keep it dynamic...

avatar image Owen-Reynolds · Apr 03, 2011 at 10:13 PM 0
Share

Same meaning. Static means one copy for the entire class, which means what you said -- if you zero out "your" health, you are really zeroing the single health we all share. Quoting your answer, above "$$anonymous$$A$$anonymous$$E SURE IT'S NOT STATIC." As long as it is public or has a public get, there won't be any "can't interact" issues.

Show more comments

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

No one has followed this question yet.

Related Questions

Need Help With My GAME! 3 Answers

Script effects all gameobjects. 1 Answer

For loop on collision (two hit kill) 2 Answers

NullReferenceException: Object reference not set to an instance of an object 4 Answers

public private and static variables in js 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