• 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 hixon33 · May 02, 2015 at 11:39 PM · javascriptinstances

code refernce on specific instance

So I have some code (lets say codeA) referencing a static variable (var) in a separate code (codeB). CodeB is applied to multiple instances. CodeA changes var and then a function in codeB is started. My problem is that I want that function to happen only in whatever instance is being affected currently, but instead it starts the function on all the instances the code is applied to. CodeA:

 var attack: int = 20;
 var enemy: GameObject;
 var enemyHealth: EnemyHealth;
 function Awake ()
 {
     enemyHealth = GetComponent(EnemyHealth);
 }
 function OnTriggerEnter(obj: Collider)
 {
     if(obj.gameObject.GetComponent(EnemyHealth))
     {
         enemyHealth.currentHealth = enemyHealth.currentHealth - attack;
     }
     
 }

CodeB:

 var startingHealth: int = 100;
 static var currentHealth: int;
 var enemy: GameObject;
 
 private var onDeath: PlayerMovement;
 private var anim: Animator;
 
 function Awake()
 {
     onDeath = GetComponent(PlayerMovement);
     currentHealth = startingHealth;
     anim = GetComponentInParent(Animator);
 }
 
 function Update()
 {
     if(currentHealth <= 0)
     {
         anim.SetBool("EnemyDeath", true);
         Destroy(enemy, 2);
         onDeath.enemyDie = true;    
     }
 }


Comment
Add comment · Show 6
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 DoTA_KAMIKADzE · May 03, 2015 at 05:14 AM 1
Share

It's kinda hard to understand what you want exactly, can you point out which "function" should be called only for 1 instance of your EnemyHealth script? Because so far I can only understand your code and you basically in your CodeA if any EnemyHealth enters Trigger then you remove chunk of their shared health and once that shared health goes down to 0 or below all of them die.

P.S. Also there is no function in your EnemyHealth script that should be called any time at all, so that's why whole your question is kinda messed up. Awake will be called on load and Update is being called every frame.

avatar image hixon33 · May 03, 2015 at 06:47 AM 0
Share

hey sorry about that, function is the wrong word I shouldn't have used it. I just meant how currentHealth is being lowered in codeA and then in codeB when its lowered enough the if statement executes. The question is basically how would I make it so that currentHealth is lowered only in the instance that is colliding with the collider on the object codeA is applied to. I'm having a hard time explaining it because my understanding of code isn't very advanced

avatar image tanoshimi · May 03, 2015 at 07:00 AM 0
Share

Why is currentHealth static? That would seem to be at least one of your problems...

avatar image hixon33 · May 03, 2015 at 07:02 AM 0
Share

currentHealth is static so that codeA can reference it. Right? Because I removed static from my script and then it wouldn't work

avatar image tanoshimi · May 03, 2015 at 07:34 AM 1
Share

No, no, no - that's not what static means. You reference other components with GetComponent: http://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DoTA_KAMIKADzE · May 03, 2015 at 07:28 AM

Now that you've said what you want:

1) Remove "static" word from line #2 in "CodeB" and make it public or internal:

 internal var currentHealth: int;

2) Then in your "CodeA":

  function OnTriggerEnter(obj: Collider)
  {
      var enH : EnemyHealth  = obj.gameObject.GetComponent(EnemyHealth);
      if(enH != null) enH.currentHealth -= attack;
  }
Comment
Add comment · Show 1 · 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 hixon33 · May 03, 2015 at 08:12 AM 0
Share

Thanks for all the help!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Prefab being altered by changes implemented to instances at run time 1 Answer

Setting Scroll View Width GUILayout 1 Answer

changing variables of all instanced Gameobject 1 Answer

Have an Int be in a Network Noob Networking Question 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