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 serenefox · Mar 11, 2013 at 05:54 AM · javascripterrorgameobjectparticleslogic

Need help with logic problem (JavaScript)

Hello,

I have been working with code all day and it burns me out so this is probably something small and/or simple that I am missing. I have two scripts that I am using in this problem of mine, a "CharacterDamage" script that keeps track of my player and enemies health and a "PlayerSmoke" script that show how damaged the player's vehicle is (instead of a health bar). Both scripts have no compile errors so I can still play the game but the PlayerSmoke script isn't doing what I want it too. I want the player's vehicle (which is the player themself actually) to start producing smoke at certain Empty GameObject's postions attached to the vehicle. I have the smoke particles and gameObjects in place already but when I take damage down to the certain health points, no smoke spawns. Here is the "CharacterDamage" Script(which I am sure is fine) that I am accessing from the "PlayerSmoke" Script:

 #pragma strict
 
 
 var hp : float;
 var replacementSpawn : GameObject[];
 var replacement : GameObject[];
 
 
 //Take Damage Function
 //Subtract damage from hp
 
 function TakeDamage( damage : float )
 {
     hp = hp - damage;
     if(hp <= 0)
     {
 
         var i : float = 0;
         
         while( i < replacementSpawn.Length)
         {
             var y : float = 0;
             while( y < replacement.Length)
             {
                 replacementSpawn[i] = Instantiate(replacement[y], transform.position, transform.rotation);
                 yield WaitForSeconds (0.09);
                 y++;
             
             }
             i++;
         }
         
         
             Destroy(gameObject);
     }
 
 }


And here is the "PlayerSmoke" script (which Im sure has the Logical error):

 #pragma strict
 
 var player : GameObject;
 var characterDamage : CharacterDamage;
 var lightSmokePrefab : GameObject;
 var lightSmokeSpawnOne : GameObject;
 var lightSmokeSpawnTwo : GameObject;
 var moderateSmokePrefab : GameObject;
 var moderateSmokeSpawn : GameObject;
 var heavySmokePrefab : GameObject;
 var heavySmokeSpawn : GameObject;
 
 function Start()
 {
     characterDamage = player.GetComponent(CharacterDamage);
 }
 
 function TakeDamage( damage : float )
 {
     characterDamage.hp = characterDamage.hp - damage;
     if(characterDamage.hp <= 80)
     {
         Instantiate(lightSmokePrefab, lightSmokeSpawnOne.transform.position, lightSmokeSpawnOne.transform.rotation);
     }
     else if(characterDamage.hp <= 60)
     {
         Instantiate(lightSmokePrefab, lightSmokeSpawnTwo.transform.position, lightSmokeSpawnTwo.transform.rotation);
     }
     else if(characterDamage.hp <= 40)
     {
         Instantiate(moderateSmokePrefab, moderateSmokeSpawn.transform.position, moderateSmokeSpawn.transform.rotation);
     }
     else if(characterDamage.hp <= 20)
     {
         Instantiate(heavySmokePrefab, heavySmokeSpawn.transform.position, heavySmokeSpawn.transform.rotation);
     }
 }

The "TakeDamage" function is from my Damage script but I also am sure that one has no problems. Thanks for looking at my question and a reply would be much appreciated.

Comment
Add comment · Show 8
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 Khada · Mar 11, 2013 at 06:04 AM 0
Share

Do you have any errors in the console window? Can you post the entire error(s) if you have any. I'm betting you have a NullReferenceException on one of those objects.

avatar image serenefox · Mar 11, 2013 at 06:07 AM 0
Share

Nope I have no errors nor warnings which I find weird too

avatar image robertbu · Mar 11, 2013 at 06:16 AM 0
Share

I don't know why you are not getting any smoke at all, but I do know that the code above will only produce smoke at a single position. Follow what happens if the health points drops below 20. As you start down, it hits "characterDamage < 80" and does that part of the if chain. Any value

Put a Debug.Log statement at the top of TakeDamage() to see if it is called, and what the value of characterDamage.hp is at that point.

avatar image Khada · Mar 11, 2013 at 06:18 AM 0
Share

Replace your PlayerSmoke script's TakeDamage method with this and see what the console says (I have a feeling you don't call the method):

 function TakeDamage( damage : float )
 {
     Debug.Log("PlayerSmoke.TakeDamage() has been called");
 
     if(characterDamage == null)
         Debug.Log("characterDamage object is null!!");
 
     if(lightSmokeSpawnOne == null)
         Debug.Log("lightSmokeSpawnOne object is null!!");
 
     if(lightSmokeSpawnTwo == null)
         Debug.Log("characterDamage object is null!!");
 
     if(moderateSmokeSpawn == null)
         Debug.Log("moderateSmokeSpawn object is null!!");
 
     if(heavySmokeSpawn == null)
         Debug.Log("heavySmokeSpawn object is null!!");

     if(lightSmokePrefab== null)
         Debug.Log("lightSmokePrefab object is null!!");

     if(moderateSmokePrefab == null)
         Debug.Log("moderateSmokePrefabobject is null!!");

     if(heavySmokePrefab== null)
         Debug.Log("heavySmokePrefabobject is null!!");
 
     characterDamage.hp = characterDamage.hp - damage;
     if(characterDamage.hp <= 80)
     {
         Instantiate(lightSmokePrefab, lightSmokeSpawnOne.transform.position, lightSmokeSpawnOne.transform.rotation);
         Debug.Log("smoke created for hp: " + characterDamage.hp);
     }
     else if(characterDamage.hp <= 60)
     {
         Instantiate(lightSmokePrefab, lightSmokeSpawnTwo.transform.position, lightSmokeSpawnTwo.transform.rotation);
         Debug.Log("smoke created for hp: " + characterDamage.hp);
     }
     else if(characterDamage.hp <= 40)
     {
         Instantiate(moderateSmokePrefab, moderateSmokeSpawn.transform.position, moderateSmokeSpawn.transform.rotation);
         Debug.Log("smoke created for hp: " + characterDamage.hp);
     }
     else if(characterDamage.hp <= 20)
     {
         Instantiate(heavySmokePrefab, heavySmokeSpawn.transform.position, heavySmokeSpawn.transform.rotation);
         Debug.Log("smoke created for hp: " + characterDamage.hp);
     }
 }
avatar image robertbu · Mar 11, 2013 at 06:21 AM 0
Share

It looks like you are taking damage off twice. Once in Character damage script and once in the PlayerSmoke script.

Show more comments

1 Reply

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

Answer by Khada · Mar 11, 2013 at 06:53 AM

Replace your PlayerSmoke script's TakeDamage method with this and see what the console says (I have a feeling you don't call the method):

 function TakeDamage( damage : float )
 {
     Debug.Log("PlayerSmoke.TakeDamage() has been called");
 
     if(characterDamage == null)
         Debug.Log("characterDamage object is null!!");
 
     if(lightSmokeSpawnOne == null)
         Debug.Log("lightSmokeSpawnOne object is null!!");
 
     if(lightSmokeSpawnTwo == null)
         Debug.Log("characterDamage object is null!!");
 
     if(moderateSmokeSpawn == null)
         Debug.Log("moderateSmokeSpawn object is null!!");
 
     if(heavySmokeSpawn == null)
         Debug.Log("heavySmokeSpawn object is null!!");

     if(lightSmokePrefab== null)
         Debug.Log("lightSmokePrefab object is null!!");

     if(moderateSmokePrefab == null)
         Debug.Log("moderateSmokePrefabobject is null!!");

     if(heavySmokePrefab== null)
         Debug.Log("heavySmokePrefabobject is null!!");
 
     characterDamage.hp = characterDamage.hp - damage;
     if(characterDamage.hp <= 80)
     {
         Instantiate(lightSmokePrefab, lightSmokeSpawnOne.transform.position, lightSmokeSpawnOne.transform.rotation);
         Debug.Log("smoke created for hp: " + characterDamage.hp);
     }
     else if(characterDamage.hp <= 60)
     {
         Instantiate(lightSmokePrefab, lightSmokeSpawnTwo.transform.position, lightSmokeSpawnTwo.transform.rotation);
         Debug.Log("smoke created for hp: " + characterDamage.hp);
     }
     else if(characterDamage.hp <= 40)
     {
         Instantiate(moderateSmokePrefab, moderateSmokeSpawn.transform.position, moderateSmokeSpawn.transform.rotation);
         Debug.Log("smoke created for hp: " + characterDamage.hp);
     }
     else if(characterDamage.hp <= 20)
     {
         Instantiate(heavySmokePrefab, heavySmokeSpawn.transform.position, heavySmokeSpawn.transform.rotation);
         Debug.Log("smoke created for hp: " + characterDamage.hp);
     }
 }
Comment
Add comment · Show 12 · 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 serenefox · Mar 11, 2013 at 06:56 AM 0
Share

But what do I do now? Nothing showed in the console or log and it still doesn't work.

avatar image Khada · Mar 11, 2013 at 07:02 AM 0
Share

It doesn't work because you never call the TakeDamage function from your PlayerSmoke script when dealing damage to the player. So either call both of your TakeDamage methods when dealing damage to the player, or have the TakeDamage method that you are calling, call the other TakeDamage method in turn.

avatar image serenefox · Mar 11, 2013 at 07:11 AM 0
Share

Im not following do you have an example by chance? Sorry Im kinda new to scripting.

avatar image Khada · Mar 11, 2013 at 07:33 AM 0
Share

You have 2 functions called TakeDamage, each existing/defined inside a different script, but you only call one of them.

Eg:

 //Script 'CharacterDamage' (on player)
 
 function TakeDamage()
 {
     //...
 }
 
 
 //Script 'PlayerSmoke' (on player)
 
 function TakeDamage()
 {
     //...
 }
 
 
 //Script X (on enemy/projectile/weapon or similar)
 
 var player : GameObject;
 
 function DealDamage()
 {
     //somewhere you are doing this or very similar
     player.GetComponent(CharacterDamage).TakeDamage();
 
     //but you're not doing this, and you should be
     player.GetComponent(PlayerSmoke).TakeDamage();
 }
avatar image serenefox · Mar 11, 2013 at 09:17 AM 1
Share

Oh cool thanks, yeah I try not to just get answers and actually prefer being pointed in the right direction and if all else fails then I'll ask, so I learn for the future. I wish more people would help people find their answers rather than just give it too them. Thats why I asked for an example earlier rather than a straight answer or script haha but sometimes I do get lazy. Ok well I'll check it later today I am going to sleep thanks for all your help tonight.

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

11 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

Related Questions

exspecting EOF, found '}'. 3 Answers

for loop error 2 Answers

OnCollisionEnter2D not working? 2 Answers

[UNSOLVED] Object reference not set to an instance of an object 0 Answers

It is not possible to invoke an expression of type 'UnityEngine.GameObject'? 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