If statement not working in script.
using UnityEngine; using System.Collections;
public class EnemyShooting : MonoBehaviour {
public Transform spawnPoint;
public Transform bulletPrefab;
public float timeBtwShots;
public float startTimeBtwShots;
// Use this for initialization
void Start () {
startTimeBtwShots = 5;
timeBtwShots = startTimeBtwShots;
}
// Update is called once per frame
void Update () {
if (GameObject.Find("RangeEnemyWhite").GetComponent<EnemyShoot>().moveSpeed == 0 || GameObject.Find("RangeEnemyBlack").GetComponent<EnemyShoot>().moveSpeed == 0)
{
timeBtwShots -= Time.deltaTime;
}
else if (GameObject.Find("RangeEnemyWhite").GetComponent<EnemyShoot>().moveSpeed == 3 || GameObject.Find("RangeEnemyBlack").GetComponent<EnemyShoot>().moveSpeed == 3)
{
timeBtwShots = startTimeBtwShots;
}
if (timeBtwShots <= 0)
{
FireBullet();
timeBtwShots = startTimeBtwShots;
}
}
void FireBullet()
{
GameObject player = GameObject.Find("Player");
if (player != null)
{
Instantiate(bulletPrefab, spawnPoint.position, spawnPoint.rotation);
}
}
}
The else if statement is not being carried out by unity for some reason, it is just ignoring it. No matter what i put in there the program does not do it. for some reason timeBtwShots wont go back to 5 when the enemies movement speed is 3.
Answer by Venrack_ · Apr 25, 2018 at 01:38 PM
If you just try and place it into another script that should fix it.
Answer by tormentoarmagedoom · Apr 25, 2018 at 09:50 AM
Good day.
When you have this kind of problem, is because some variable value is not as you think it is. The best you can do is to add a
Debug.Log();
before the else if sentence, with all the variables that are included, in your case, i recommend to paste this code, and check in the console, what is the variable value.
void Update () {
Debug.Log (GameObject.Find("RangeEnemyWhite").GetComponent<EnemyShoot>().moveSpeed);
Debug.Log (GameObject.Find("RangeEnemyBlack").GetComponent<EnemyShoot>().moveSpeed);
if (GameObject.Find("RangeEnemyWhite").GetComponent<EnemyShoot>().moveSpeed == 0 || GameObject.Find("RangeEnemyBlack").GetComponent<EnemyShoot>().moveSpeed == 0)
{
timeBtwShots -= Time.deltaTime;
}
else if (GameObject.Find("RangeEnemyWhite").GetComponent<EnemyShoot>().moveSpeed == 3 || GameObject.Find("RangeEnemyBlack").GetComponent<EnemyShoot>().moveSpeed == 3)
{
timeBtwShots = startTimeBtwShots;
}
if (timeBtwShots <= 0)
{
FireBullet();
timeBtwShots = startTimeBtwShots;
}
}
I'm sure some of them is null, empty or does not have the value you think it have.
If helped, accept the answer and close the question!
BYE !:D
@tormentoarmagedoom that didn't work i think the problem is that unity is flat out not registering that if statement at all no matter what i put in it its not reading it its happening on other scripts too with different situations. I just dont know how to fix it?
Answer by vinilly · Apr 25, 2018 at 10:21 AM
try to reset the code with its on variable.
timeBtwShots = 5;
as the script is out of start mode and startTimeBtwShots might be null
else if (GameObject.Find("RangeEnemyWhite").GetComponent<EnemyShoot>().moveSpeed == 3 || GameObject.Find("RangeEnemyBlack").GetComponent<EnemyShoot>().moveSpeed == 3)
{
timeBtwShots = startTimeBtwShots;
}
if (timeBtwShots <= 0)
{
FireBullet();
//timeBtwShots = startTimeBtwShots; //null
timeBtwShots = 5; // New!
}
Please vote up to keep me around.:D
Your answer
Follow this Question
Related Questions
A* Pathfinding for Top Down project, how do I make enemyAI to rotate towards waypoints? 0 Answers
how to make an enemy flash when clicked on? 0 Answers
Help with raycast for for wall detection 1 Answer
Stop enemy from chasing at a certain point 0 Answers
Pushing enemy makes them stop following the player 0 Answers