Function not running everytime
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
|
show 6 more comments
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
EitherTakeDamage
is not being called, orhealth
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 ofGetComponent()
does not exist, this code seems incomplete to me. What components are you trying to retrieve withGetComponent()
?
– 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
|
show 6 more comments
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
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
c# unity3d
edited Nov 19 '18 at 22:53
Ruzihm
3,51111627
3,51111627
asked Nov 19 '18 at 16:15
HaosDEW
1
1
EitherTakeDamage
is not being called, orhealth
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 ofGetComponent()
does not exist, this code seems incomplete to me. What components are you trying to retrieve withGetComponent()
?
– 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
|
show 6 more comments
EitherTakeDamage
is not being called, orhealth
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 ofGetComponent()
does not exist, this code seems incomplete to me. What components are you trying to retrieve withGetComponent()
?
– 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
|
show 6 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Either
TakeDamage
is not being called, orhealth
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 withGetComponent()
?– 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