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 samaxi · Sep 16, 2013 at 12:12 AM · javascriptfpsvariable

Static var wont work

The enemy health code I am using

Characterdamage2.js

 static var hitPoints = 100.0;
 var deadReplacement : Transform;
 var dieSound : AudioClip;
 
 function ApplyDamage (damage : float) {
     // We already have less than 0 hitpoints, maybe we got killed already?
     if (hitPoints <= 0.0)
         return;
 
     hitPoints -= damage;
     if (hitPoints <= 0.0)
     {
         Detonate();
     }
 }
 
 function Detonate () {
     // Destroy ourselves
     Destroy(gameObject);
     
     // Play a dying audio clip
     if (dieSound)
         AudioSource.PlayClipAtPoint(dieSound, transform.position);
 
     // Replace ourselves with the dead body
     if (deadReplacement) {
         var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
         
         // Copy position & rotation from the old hierarchy into the dead replacement
         CopyTransformsRecurse(transform, dead);
     }
 }
 
 static function CopyTransformsRecurse (src : Transform,  dst : Transform) {
     dst.position = src.position;
     dst.rotation = src.rotation;
     
     for (var child : Transform in dst) {
         // Match the transform with the same name
         var curSrc = src.Find(child.name);
         if (curSrc)
             CopyTransformsRecurse(curSrc, child);
     }
 }

Now this code in my spawning script wont work, it makes the enemies what seems like invincible

 CharacterDamage2.hitPoints += 10.0;

my entire spawn code is this

 var textColor = Color.red;
 var NumberEnemies : int;
 static var WaveNumber = 0;
 var NewWave : boolean = false;
 static var score = 0;
 
 var SpawnPoints : Transform[];
 var Enemies : Transform[];
 
 function Awake () {
 guiText.material.color = textColor;
 }
 
 function Update(){
 NumberEnemies = ((GameObject.FindGameObjectsWithTag("Enemy").Length) - 1);
 
 
 if(NumberEnemies == 0 && !NewWave){
 NewWave = true;
 WaveNumber += 1;
 SpawnEnemies();
 }
 }
 
 function SpawnEnemies(){
 yield WaitForSeconds (3);
 CharacterDamage2.hitPoints += 1.0;
 SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[0].position,SpawnPoints[0].rotation);
 SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[1].position,SpawnPoints[1].rotation);
 SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[2].position,SpawnPoints[2].rotation);
 SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[3].position,SpawnPoints[3].rotation);
 SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[4].position,SpawnPoints[4].rotation);
 NewWave = false;
 
 }
 
 function OnGUI () {
     GUI.Box(Rect(10,15,75,35), "Score: " +score);
     GUI.Box(Rect(10,105,75,35), "Wave: " +WaveNumber);
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Eric5h5 · Sep 16, 2013 at 12:35 AM

You can't use static vars like that; static means there's only one instance of that variable regardless of how many objects are using the script. Remove static from all variables unless you actually mean for there to be one instance and fully understand all the consequences of using static.

Comment
Add comment · Show 6 · 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 samaxi · Sep 16, 2013 at 10:48 AM 0
Share

Oh okay, is there anyway I can have it so after every wave the enemy gains 10 health?

avatar image Eric5h5 · Sep 16, 2013 at 02:33 PM 0
Share

Of course, just increase the health variable by 10.

avatar image samaxi · Sep 16, 2013 at 09:52 PM 0
Share

How though, that's what I was trying to do.

avatar image Eric5h5 · Sep 16, 2013 at 09:55 PM 0
Share

http://docs.unity3d.com/356/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

avatar image samaxi · Sep 17, 2013 at 10:46 AM 0
Share

What am I doing wrong? var CharacterDamage2: CharacterDamage2 = hitpoints(CharacterDamage2); CharacterDamage2.hitPoints += 1.0();

Show more comments
avatar image
0

Answer by oliver-jones · Sep 16, 2013 at 09:58 PM

You need to access your health variable WITHOUT using a static variable. Just remove the 'static' bit, and you should be good to go. If you need to actually access your health variable externally, then you need to use GetComponent();

Comment
Add comment · Show 3 · 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 samaxi · Sep 17, 2013 at 12:07 AM 0
Share

I did this and got "An instance of type 'CharacterDamage2' is requited to access non static member 'hitpoints'.

avatar image oliver-jones · Sep 17, 2013 at 08:14 AM 0
Share

Thats because you're trying to access it 'statically' here:

 function SpawnEnemies(){
 yield WaitForSeconds (3);
 CharacterDamage2.hitPoints += 1.0;

You can't access it as a static (well, you shouldn't as thats causing your problem), as Eric mentioned, you want to use the link he has provided.

avatar image samaxi · Sep 17, 2013 at 08:28 PM 0
Share

What am I doing wrong? var CharacterDamage2: CharacterDamage2 = hitpoints(CharacterDamage2); CharacterDamage2.hitPoints += 1.0();

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Accessing other script problem 1 Answer

Temperary Variables 0 Answers

How do I deselect an object when another is selected? 2 Answers

Unity mathematics phrasing. 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