- Home /
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.
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.
Nope I have no errors nor warnings which I find weird too
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.
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);
}
}
It looks like you are taking damage off twice. Once in Character damage script and once in the PlayerSmoke script.
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);
}
}
But what do I do now? Nothing showed in the console or log and it still doesn't work.
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.
Im not following do you have an example by chance? Sorry Im kinda new to scripting.
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();
}
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.
Your answer
Follow this Question
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