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 Xatoku · Mar 02, 2011 at 03:14 AM · variablestatic

Static, yet Private, Variables?

I have my script done so that it spawns enemies which the player can fight and they will attack him. The problem is that because the enemies use the same script there's no way to indicate if the player's health has gone down without the collision defining whether he's been hit or not is static. Don't get me? Here's what I mean:

If 2 enemies are attacking the Player, because the hitTime is static, only 1 will hit him.

How can I change it so that both enemies will hit the Player?

Variable

static var hitTimeWeak:boolean = false;

Enemy Script:

function Attack()
{
    isAttacking = true;
    if(isAttacking == true && beingHit == false)
    {
        yield WaitForSeconds(WeakAttackSpeed);
        hitTimeWeak = true;
        animation.CrossFade("attack1");
        yield WaitForSeconds(.5);
        hitTimeWeak = false;
        yield WaitForSeconds(.8);
        animation.Stop();
        hitTimeWeak = false;
        isAttacking = false;
    }else{
        isAttacking = false;
    }
}

Player Getting Hit:

function OnTriggerEnter(hitWeak : Collider)
{
    if(hitWeak.gameObject.tag == "StalfosWeak" && Enemy1.hitTimeWeak == true)
    {
        HealthBar.HEALTH -= 1;
    }
}

Comment
Add comment
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

1 Reply

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

Answer by Eric5h5 · Mar 02, 2011 at 03:38 AM

You can't use static variables like that; static means that only one instance can ever exist. Don't use static variables unless you mean for that to happen. So remove "static" and just use normal variables, in which case each instance of the script will have its own independent variables.

Comment
Add comment · Show 5 · 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 Xatoku · Mar 02, 2011 at 04:02 AM 0
Share

I understand, but then whats the alternative in order to make each enemy hit him?

avatar image Eric5h5 · Mar 02, 2011 at 04:16 AM 0
Share

@Jordan: You don't need an alternative, just remove "static". Unless you're talking about accessing other scripts, in which case http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

avatar image Xatoku · Mar 02, 2011 at 05:02 AM 0
Share

What I mean is, because all enemies share the script, they will all share static variables, so If one's attacking, they're all attacking, and if I take out the static part, then I can't access it from the character script to inform him that he's been hit and needs to take damage. Currently, if they hit at the same time, 1 will hit, but the other won't because the hitTimeWeak variable is in use.

avatar image Eric5h5 · Mar 02, 2011 at 05:42 AM 0
Share

@Jordan: As I said, you have to remove "static" from the variables and make them non-static. You can't use static variables if you need more than once instance. You can easily access non-static variables from other scripts, see the link I just posted.

avatar image Xatoku · Mar 02, 2011 at 06:21 AM 0
Share

So this will tell me if at anytime the hitTimeSmall variable in the Enemy1 script is true, the character will get hurt, if applied correctly?

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

Manipulating Variables Over Multiple Scripts 0 Answers

Prefab's Script affecting all the Prefab 1 Answer

Static variable not changing from outside class 2 Answers

can i change a variable from non-static to static at runtime in c#? 1 Answer

Unknown identifier, OnCollisionEnter Error 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