My function is being called even when it shouldn't
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameController : MonoBehaviour { public GameObject enemy; public float setEnemyLinearSpeed; public float setEnemySideSpeed; public float setEnemyFollowSpeed; public float setEnemyJumpForce; public float setEnemyJumpFrequency; public float setEnemyShootFrequency; public float setHighestPointOfJump; public float setDownfallRate; public float setEnemyHealth; public float enemySpawnFrequency;
private int decidingNumber;
void Start() {
RevertPrefabValuesToNormal ();
print ("setting everything to normal");
InvokeRepeating ("DecideWhatEnemyToSpawn", 1f, enemySpawnFrequency);
}
void DecideWhatEnemyToSpawn() {
// decidingNumber = Random.Range (0, 5); decidingNumber = 0; SetDefaultProperties (setEnemyLinearSpeed, setEnemyHealth);
switch (decidingNumber) {
case 0:
print ("case 0");
break;
case 1:
print ("case 1");
SetSideSpeed (setEnemySideSpeed, true);
break;
case 2:
print ("case 2");
JumpParameters (setEnemyJumpForce, setEnemyJumpFrequency, setHighestPointOfJump, setDownfallRate, true);
break;
case 3:
print ("case 3");
SetFollowSpeed (setEnemyFollowSpeed, true);
break;
case 4:
print ("case 4");
EnemyShootFrequency (setEnemyShootFrequency, true);
break;
}
Instantiate(enemy, new Vector3(transform.position.x, transform.position.y, Random.Range(15f, 38f)), transform.rotation);
RevertPrefabValuesToNormal ();
print ("everything should be back to normal again");
}
void RevertPrefabValuesToNormal() {
SetDefaultProperties (0, 0);
SetSideSpeed (0, false);
SetFollowSpeed (0, false);
JumpParameters (0, 0, 0, 0, false);
EnemyShootFrequency (0, false);
print("everything is normal now");
}
void SetDefaultProperties(float _setEnemyLinearSpeed, float _setEnemyHealth) {
enemy.GetComponent<EnemyController> ().linearSpeed = _setEnemyLinearSpeed;
enemy.GetComponent<EnemyController> ().health = _setEnemyHealth;
print ("setting default");
}
void SetSideSpeed(float _setEnemySideSpeed, bool _canGoSide) {
enemy.GetComponent<EnemyController> ().sideSpeed = _setEnemySideSpeed;
enemy.GetComponent<EnemyController> ().canGoSide = _canGoSide;
}
void SetFollowSpeed(float _setEnemyFollowSpeed, bool _canFollow) {
enemy.GetComponent<EnemyController> ().followSpeed = _setEnemyFollowSpeed;
enemy.GetComponent<EnemyController> ().canFollow = _canFollow;
}
void JumpParameters(float _setEnemyJumpForce, float _setEnemyJumpFrequency, float _setHighestPointOfJump, float _setDownfallRate, bool _canJump) {
enemy.GetComponent<EnemyController> ().jumpForce = setEnemyJumpForce;
enemy.GetComponent<EnemyController> ().jumpFrequency = setEnemyJumpFrequency;
enemy.GetComponent<EnemyController> ().highestPointOfJump = setHighestPointOfJump;
enemy.GetComponent<EnemyController> ().downwardFallRate = setDownfallRate;
enemy.GetComponent<EnemyController> ().canJump = true;
print ("assigning jump");
}
void EnemyShootFrequency(float _setEnemyShootFrequency, bool _canShoot) {
enemy.GetComponent<EnemyController> ().shootFrequency = setEnemyShootFrequency;
enemy.GetComponent<EnemyController> ().canShoot = true;
print ("assigning shoot");
}
}
As you can clearly see, the SetJumpParameters() and SetEnemyShootFrequency() is being called even when they shouldn't be. Why is that?
Sorry the code is messy and not very easy to read, so it's not clear what you are having problems with exactly? Can you edit the post to display the code properly?
You say we can clearly see that setjumpparameters() and setenemyshootfrequency() is being called, but the setjumpparameters method isn't referenced anywhere in the above code, and the setenemyshootfrequency only seems to be referenced as a variable that must be modified elsewhere, because I can't see where it has been set? If you can include all relevant code, format it so it's easy to read, and be clear on what is happening, what you expect to happen, and what you have tried so far, somebody may be able to help.
The guys able to offer their time and assistance don't want to sit debugging pages and pages of somebody else's code, so you'll likely get a much quicker response if you clarify the details as suggested above. I hope that helps.
Have you found the answer to this? If you have please tell me I am having the same problem. I can't post the code because it is private, but it is similar to yours. If I find the answer I will tell you if you don't know the answer already.
Your answer

Follow this Question
Related Questions
Call function of a MonoBehaviour class in another class 0 Answers
Using variables without overflow 0 Answers
Augmented reality possible functions? 0 Answers
Calling Function from another script not working 1 Answer
Method Group 1 Answer