If statement is being ignored
Hello all,
I am making an FPS game where in this case, I am programming the enemy to hide when it finds the closest cover point and to shoot me when I get closer to it. However, I want the enemy to shoot in random directions around me to make it more realistic but when the enemy approaches the cover position this specific if statement (line 78) doesn't even execute, but on another enemy and using the same code it does execute. Is there a reason why? Why does Unity3D ignores some lines of code?
pragma strict
var health = 50;
var player:GameObject;
var EnemyDamage = 10;
var bulletHoleObject:GameObject;
var ShootPoint:GameObject; //The point where the enemy will shoot
var effect: Transform;
var currentTime = Time.time;
var enemyShootCount = 0;
var IfGetNumberOfShootsCalled = false;
var numberOfShoots:int;
//weapon
var clip:int;
var cover:GameObject;
//Enemy cover system
var LoockedForTheFirstTime = false;
//Enemy navigation (nav mesh properties)
var agent: NavMeshAgent;
function DeductPoints(WeaponDamage: int){
health = health - WeaponDamage;
}
function Start(){
clip = 30;
agent = GetComponent(NavMeshAgent);
}
function Update () {
var Shot:RaycastHit;
var PointToPlayer:RaycastHit;
var UserPlayerPosition:Vector3 = player.transform.position - transform.position;
//Debug.DrawRay(transform.position, UserPlayerPosition * 1000, Color.red);
var distanceBetweenEnemyAndPlayer = Vector3.Distance (transform.position, player.transform.position); //The distance between the player and the enemy
var EnemyAndClosestCoverDistance = Vector3.Distance(transform.position, cover.transform.position); //The distance between the enemy and the cover (We use it to get the closest distance between an enemy and a cover)
// Shooting properties && ammo with reload
var RandomShoot:Vector3;
//covering
if (distanceBetweenEnemyAndPlayer <= 50 || LoockedForTheFirstTime == true){
if ((EnemyAndClosestCoverDistance < 50 || LoockedForTheFirstTime == true) && cover.GetComponent(coverProperties).availability == true){ //Checks if the distance between a cover point and a bot is less than 50
// transform.position = Vector3.MoveTowards(transform.position, cover.transform.position, Time.deltaTime*7); //moves the bot directly to the cover point
agent.SetDestination(cover.transform.position);
LoockedForTheFirstTime = true;
}
}
//---end of covering---
if (distanceBetweenEnemyAndPlayer <= 35){
gameObject.GetComponent(Renderer).material.color = Color.red;
}
else {
gameObject.GetComponent(Renderer).material.color = Color.white;
}
//Shooting properties
if ((Time.time - currentTime >= 0.1) && (Physics.Raycast(ShootPoint.transform.position, UserPlayerPosition, Shot)) && distanceBetweenEnemyAndPlayer <= 50){
Physics.Raycast(ShootPoint.transform.position, UserPlayerPosition, PointToPlayer); //Creates a Ray which points the Player all the time.
if (PointToPlayer.collider.gameObject.tag == "Player"){ //checks if the PointToPlayer points on the Player (
//The above if statement is useful because we want the bot to shoot the player only when the PointToPlayer is pointing on the player only.
if (clip <= 0){
currentTime = Time.time + 2.5;
enemyShootCount = 0;
IfGetNumberOfShootsCalled = false;
clip = 30;
}
if (IfGetNumberOfShootsCalled == false){
numberOfShoots = GetNumberOfShoots();
IfGetNumberOfShootsCalled = true;
}
if (enemyShootCount <= numberOfShoots){ //`**<---- this if statement is being ignored**`
print("Hello");
RandomShoot = Vector3(Random.Range(Shot.distance/(-10),Shot.distance/10),Random.Range(Shot.distance/(-10),Shot.distance/10),0);
Debug.DrawRay(ShootPoint.transform.position, UserPlayerPosition * 1000, Color.red);
enemyShootCount = enemyShootCount + 1;
clip = clip - 1;
}
if (enemyShootCount == numberOfShoots){
currentTime = Time.time + 0.5;
enemyShootCount = 0;
IfGetNumberOfShootsCalled = false; //enabling access to the second if statement, so we can call the GetNumberOfShoots function
}
}
}
//----End of shooting properties---
//Actual shooting
if(Physics.Raycast(ShootPoint.transform.position, UserPlayerPosition - RandomShoot, Shot) && Time.time - currentTime >= 0.1 && distanceBetweenEnemyAndPlayer <= 50){
Physics.Raycast(transform.position, UserPlayerPosition, PointToPlayer); //Creates a Ray which points the Player all the time.
if (PointToPlayer.collider.gameObject.tag == "Player"){ //checks if the PointToPlayer points on the Player (
//The above if statement is useful because we want the bot to shoot the player only when the PointToPlayer is pointing on the player only.
var particleClone = Instantiate(effect, Shot.point, Quaternion.FromToRotation(Vector3(0,0,0),Shot.normal));
Destroy(particleClone.gameObject, 2);
var bulletHole = Instantiate(bulletHoleObject, Shot.point, Quaternion.FromToRotation(Vector3.up, Shot.normal));
Destroy(bulletHole.gameObject, 60);
GetComponent.<AudioSource>().Play();
print(RandomShoot);
if (Shot.collider.gameObject.tag == "Player"){
Shot.transform.SendMessage("UserPlayerHealth" ,EnemyDamage ,SendMessageOptions.DontRequireReceiver);
}
currentTime = Time.time;
}
}
//----End of actual shooting----
if (health <= 0){
Destroy(gameObject);
}
}
function GetNumberOfShoots(){
return Random.Range(5,15);
}
Answer by tanoshimi · Aug 16, 2016 at 03:55 PM
If you're not sure whether you've found a bug in Unity or in your code, it's 99.9999% likely to be in your code. What makes you think the if
statement is not being executed? Are you expecting it to return true or false? Test your code by using the following:
Debug.Log("enemyShootCount is " + enemyShootCount.ToString() + " and numberOfShoots is " + numberOfShoots.ToString());
if (enemyShootCount <= numberOfShoots){
It doesn't execute when I move closer to the other player.In that case, the RandomShoot should be updated but it stays on 0,0,0. Also, the piece of code between the //Actual shooting and //----End of actual shooting---- doesn't execute well. I mean that I won't hear the shooting sound from this piece of code:
GetComponent.<AudioSource>().Play();
as many times as the enemyShootCount number. For example, the numberOfShoots will store 8 but, I will listen to the shoot sound only 3 times. That's the issue I am currently facing. Also, I tried to convert Javascript to C# because C# compiles faster than Javascript, I still have the same error.
Sorry for the bad English but English is not my first language.
I appreciate your help.
If Unity randomly ignored lines of code, it would be pretty pointless to try to make anything with it.
That print("Hello");
is the only line of console output in your code. How can you know that you even get to that line (on the times you think it's ignored)? $$anonymous$$aybe shootCount or numberOfShoots get wrong values sometimes? $$anonymous$$aybe you don't even get inside the (2?) enclosing ifs? $$anonymous$$aybe a raycist fails sometimes.
All you can do is debug those things.
Your answer

Follow this Question
Related Questions
If conditions doesn't work?!? 0 Answers
The If statement condition is false but the if statement stills executes 1 Answer
Why 2d jump not working on touch? 0 Answers
Is There A Way To Make If Statement Condition Shorter 3 Answers
How do i make a sword only do damage to an enemy when the left mouse button is clicked? 1 Answer