Function not running everytime












0














I learn by creating a simple game, something like space invaders. And the problem that i have is sometimes a script is executed and sometimes its not. Let me show you the code:



First my player fires a gun, it instantiates a prefab of bullet.



Fire a gun:



void Update() { 
if (Input.GetMouseButtonDown(0)) {
Instantiate(rocket, strzal.position, strzal.rotation);
}
}


Bullet script:



private void OnTriggerEnter2D(Collider2D collision) { 
if (collision.gameObject.tag== "Przeszkoda"){
collision.GetComponent().TakeDamage(damage);
Destroy(gameObject);
}
}


Then if the bullet hits a target it triggers a damage function:



public void TakeDamage(float amount) { 
health -= amount;
if (health <= 0) {
ABC.GetComponent<DEF>().NowyPoziomTrudnosci();
gameObject.SetActive(false);


And so far everything is working just fine. But problem starts with NowyPoziomTrudnosci function. Sometimes its running then 3 enemies are killed and nothing happens and 2 next enemies run the code and again x enemies die and code is not executing.



public void NowyPoziomTrudnosci() { 
kills = kills + 1;
Debug.Log("wykonane"+ kills);
}


Any idea where I made a mistake? I would understand if NowyPoziomTrudnosci wouldn't run at all. But it's running, just not every time.










share|improve this question
























  • Either TakeDamage is not being called, or health is not <= 0. Have you stepped through the code? That should show you why the line isn't executing.
    – Sam W
    Nov 19 '18 at 16:17












  • I suspect something about that gameObject.SetActive(false);. What happen if you comment it for a test?
    – Andrea
    Nov 19 '18 at 16:22








  • 2




    Maybe I am blind, but what is ABC in this code? Also, an overloaded method of GetComponent() does not exist, this code seems incomplete to me. What components are you trying to retrieve with GetComponent()?
    – Max Play
    Nov 19 '18 at 16:26










  • @Maxplay ABC is my gamemanager object. I wanted it to store data about killed enemies and when there is enough enemies killed to increase the difficulty of the game. Code of ABC goes like this: public class DEF : MonoBehaviour { public float poziomTrudnosci = 1; private float kills = 0; public void NowyPoziomTrudnosci() { kills = kills + 1; Debug.Log("wykonane"+ kills); }}
    – HaosDEW
    Nov 19 '18 at 16:39






  • 1




    Please have a look at How to create a Minimal, Complete, and Verifiable example
    – derHugo
    Nov 19 '18 at 17:20
















0














I learn by creating a simple game, something like space invaders. And the problem that i have is sometimes a script is executed and sometimes its not. Let me show you the code:



First my player fires a gun, it instantiates a prefab of bullet.



Fire a gun:



void Update() { 
if (Input.GetMouseButtonDown(0)) {
Instantiate(rocket, strzal.position, strzal.rotation);
}
}


Bullet script:



private void OnTriggerEnter2D(Collider2D collision) { 
if (collision.gameObject.tag== "Przeszkoda"){
collision.GetComponent().TakeDamage(damage);
Destroy(gameObject);
}
}


Then if the bullet hits a target it triggers a damage function:



public void TakeDamage(float amount) { 
health -= amount;
if (health <= 0) {
ABC.GetComponent<DEF>().NowyPoziomTrudnosci();
gameObject.SetActive(false);


And so far everything is working just fine. But problem starts with NowyPoziomTrudnosci function. Sometimes its running then 3 enemies are killed and nothing happens and 2 next enemies run the code and again x enemies die and code is not executing.



public void NowyPoziomTrudnosci() { 
kills = kills + 1;
Debug.Log("wykonane"+ kills);
}


Any idea where I made a mistake? I would understand if NowyPoziomTrudnosci wouldn't run at all. But it's running, just not every time.










share|improve this question
























  • Either TakeDamage is not being called, or health is not <= 0. Have you stepped through the code? That should show you why the line isn't executing.
    – Sam W
    Nov 19 '18 at 16:17












  • I suspect something about that gameObject.SetActive(false);. What happen if you comment it for a test?
    – Andrea
    Nov 19 '18 at 16:22








  • 2




    Maybe I am blind, but what is ABC in this code? Also, an overloaded method of GetComponent() does not exist, this code seems incomplete to me. What components are you trying to retrieve with GetComponent()?
    – Max Play
    Nov 19 '18 at 16:26










  • @Maxplay ABC is my gamemanager object. I wanted it to store data about killed enemies and when there is enough enemies killed to increase the difficulty of the game. Code of ABC goes like this: public class DEF : MonoBehaviour { public float poziomTrudnosci = 1; private float kills = 0; public void NowyPoziomTrudnosci() { kills = kills + 1; Debug.Log("wykonane"+ kills); }}
    – HaosDEW
    Nov 19 '18 at 16:39






  • 1




    Please have a look at How to create a Minimal, Complete, and Verifiable example
    – derHugo
    Nov 19 '18 at 17:20














0












0








0







I learn by creating a simple game, something like space invaders. And the problem that i have is sometimes a script is executed and sometimes its not. Let me show you the code:



First my player fires a gun, it instantiates a prefab of bullet.



Fire a gun:



void Update() { 
if (Input.GetMouseButtonDown(0)) {
Instantiate(rocket, strzal.position, strzal.rotation);
}
}


Bullet script:



private void OnTriggerEnter2D(Collider2D collision) { 
if (collision.gameObject.tag== "Przeszkoda"){
collision.GetComponent().TakeDamage(damage);
Destroy(gameObject);
}
}


Then if the bullet hits a target it triggers a damage function:



public void TakeDamage(float amount) { 
health -= amount;
if (health <= 0) {
ABC.GetComponent<DEF>().NowyPoziomTrudnosci();
gameObject.SetActive(false);


And so far everything is working just fine. But problem starts with NowyPoziomTrudnosci function. Sometimes its running then 3 enemies are killed and nothing happens and 2 next enemies run the code and again x enemies die and code is not executing.



public void NowyPoziomTrudnosci() { 
kills = kills + 1;
Debug.Log("wykonane"+ kills);
}


Any idea where I made a mistake? I would understand if NowyPoziomTrudnosci wouldn't run at all. But it's running, just not every time.










share|improve this question















I learn by creating a simple game, something like space invaders. And the problem that i have is sometimes a script is executed and sometimes its not. Let me show you the code:



First my player fires a gun, it instantiates a prefab of bullet.



Fire a gun:



void Update() { 
if (Input.GetMouseButtonDown(0)) {
Instantiate(rocket, strzal.position, strzal.rotation);
}
}


Bullet script:



private void OnTriggerEnter2D(Collider2D collision) { 
if (collision.gameObject.tag== "Przeszkoda"){
collision.GetComponent().TakeDamage(damage);
Destroy(gameObject);
}
}


Then if the bullet hits a target it triggers a damage function:



public void TakeDamage(float amount) { 
health -= amount;
if (health <= 0) {
ABC.GetComponent<DEF>().NowyPoziomTrudnosci();
gameObject.SetActive(false);


And so far everything is working just fine. But problem starts with NowyPoziomTrudnosci function. Sometimes its running then 3 enemies are killed and nothing happens and 2 next enemies run the code and again x enemies die and code is not executing.



public void NowyPoziomTrudnosci() { 
kills = kills + 1;
Debug.Log("wykonane"+ kills);
}


Any idea where I made a mistake? I would understand if NowyPoziomTrudnosci wouldn't run at all. But it's running, just not every time.







c# unity3d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 22:53









Ruzihm

3,51111627




3,51111627










asked Nov 19 '18 at 16:15









HaosDEW

1




1












  • Either TakeDamage is not being called, or health is not <= 0. Have you stepped through the code? That should show you why the line isn't executing.
    – Sam W
    Nov 19 '18 at 16:17












  • I suspect something about that gameObject.SetActive(false);. What happen if you comment it for a test?
    – Andrea
    Nov 19 '18 at 16:22








  • 2




    Maybe I am blind, but what is ABC in this code? Also, an overloaded method of GetComponent() does not exist, this code seems incomplete to me. What components are you trying to retrieve with GetComponent()?
    – Max Play
    Nov 19 '18 at 16:26










  • @Maxplay ABC is my gamemanager object. I wanted it to store data about killed enemies and when there is enough enemies killed to increase the difficulty of the game. Code of ABC goes like this: public class DEF : MonoBehaviour { public float poziomTrudnosci = 1; private float kills = 0; public void NowyPoziomTrudnosci() { kills = kills + 1; Debug.Log("wykonane"+ kills); }}
    – HaosDEW
    Nov 19 '18 at 16:39






  • 1




    Please have a look at How to create a Minimal, Complete, and Verifiable example
    – derHugo
    Nov 19 '18 at 17:20


















  • Either TakeDamage is not being called, or health is not <= 0. Have you stepped through the code? That should show you why the line isn't executing.
    – Sam W
    Nov 19 '18 at 16:17












  • I suspect something about that gameObject.SetActive(false);. What happen if you comment it for a test?
    – Andrea
    Nov 19 '18 at 16:22








  • 2




    Maybe I am blind, but what is ABC in this code? Also, an overloaded method of GetComponent() does not exist, this code seems incomplete to me. What components are you trying to retrieve with GetComponent()?
    – Max Play
    Nov 19 '18 at 16:26










  • @Maxplay ABC is my gamemanager object. I wanted it to store data about killed enemies and when there is enough enemies killed to increase the difficulty of the game. Code of ABC goes like this: public class DEF : MonoBehaviour { public float poziomTrudnosci = 1; private float kills = 0; public void NowyPoziomTrudnosci() { kills = kills + 1; Debug.Log("wykonane"+ kills); }}
    – HaosDEW
    Nov 19 '18 at 16:39






  • 1




    Please have a look at How to create a Minimal, Complete, and Verifiable example
    – derHugo
    Nov 19 '18 at 17:20
















Either TakeDamage is not being called, or health is not <= 0. Have you stepped through the code? That should show you why the line isn't executing.
– Sam W
Nov 19 '18 at 16:17






Either TakeDamage is not being called, or health is not <= 0. Have you stepped through the code? That should show you why the line isn't executing.
– Sam W
Nov 19 '18 at 16:17














I suspect something about that gameObject.SetActive(false);. What happen if you comment it for a test?
– Andrea
Nov 19 '18 at 16:22






I suspect something about that gameObject.SetActive(false);. What happen if you comment it for a test?
– Andrea
Nov 19 '18 at 16:22






2




2




Maybe I am blind, but what is ABC in this code? Also, an overloaded method of GetComponent() does not exist, this code seems incomplete to me. What components are you trying to retrieve with GetComponent()?
– Max Play
Nov 19 '18 at 16:26




Maybe I am blind, but what is ABC in this code? Also, an overloaded method of GetComponent() does not exist, this code seems incomplete to me. What components are you trying to retrieve with GetComponent()?
– Max Play
Nov 19 '18 at 16:26












@Maxplay ABC is my gamemanager object. I wanted it to store data about killed enemies and when there is enough enemies killed to increase the difficulty of the game. Code of ABC goes like this: public class DEF : MonoBehaviour { public float poziomTrudnosci = 1; private float kills = 0; public void NowyPoziomTrudnosci() { kills = kills + 1; Debug.Log("wykonane"+ kills); }}
– HaosDEW
Nov 19 '18 at 16:39




@Maxplay ABC is my gamemanager object. I wanted it to store data about killed enemies and when there is enough enemies killed to increase the difficulty of the game. Code of ABC goes like this: public class DEF : MonoBehaviour { public float poziomTrudnosci = 1; private float kills = 0; public void NowyPoziomTrudnosci() { kills = kills + 1; Debug.Log("wykonane"+ kills); }}
– HaosDEW
Nov 19 '18 at 16:39




1




1




Please have a look at How to create a Minimal, Complete, and Verifiable example
– derHugo
Nov 19 '18 at 17:20




Please have a look at How to create a Minimal, Complete, and Verifiable example
– derHugo
Nov 19 '18 at 17:20












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378701%2ffunction-not-running-everytime%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378701%2ffunction-not-running-everytime%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory