Click here to Skip to main content
14,668,823 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have two towers that fire bullets, I have an enemy script with my code detecting bullet collisions, if hit by electric, the enemy gets electrocuted, if hit by water bomb, play hit by water animation and with certain take damage for each bullet,


water bomb works but electric doesn't

if I delete the second part of code about the water bomb if n else then electric works

What I have tried:

void OnTriggerEnter2D (Collider2D other)
{
    if (other.gameObject.CompareTag("bullet"))
    {
        SkelAnim.state.SetAnimation(0,electrick,true);

        moveScript.WalkSpeed = 0f;

        TakeDamgeEnemy (15f);

    }else{

        electrickcuted -= Time.deltaTime;

        SkelAnim.state.SetAnimation(0,walk,true);

        moveScript.WalkSpeed = 1f;
    }
    if (other.gameObject.CompareTag("WaterBomb"))
    {
        SkelAnim.state.SetAnimation(0,stand,true);

        moveScript.WalkSpeed = 0f;

        TakeDamgeEnemy (50f);
    }else{
        electrickcuted -= Time.deltaTime;

        SkelAnim.state.SetAnimation(0,walk,true);

        moveScript.WalkSpeed = 1f;
    }
}
Posted
Updated 30-Jan-19 9:42am
v8

Rate this:
Please Sign up or sign in to vote.

Solution 1

You must debug your code yourself, but I think that you repeatingly are calling this handler. Maybe via some callback or timer.

I would remove the bullet from the game object or model when it is handled.

Tip: make some output on the console.
   
Comments
Member 14135033 30-Jan-19 12:39pm
   
Thank you for your reply I have Debug.Log("hit by bullet"); and Debug.Log("hit by water"); thats working , its just doing the first collison it detected , then not the other , if i switch the towers around then the other one works
Rate this:
Please Sign up or sign in to vote.

Solution 2

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
void OnTriggerEnter2D (Collider2D other)
{
    if (other.gameObject.CompareTag("bullet"))
    {
        SkelAnim.state.SetAnimation(0,electrick,true);

        moveScript.WalkSpeed = 0f;

        TakeDamgeEnemy (15f);

    }else{

        electrickcuted -= Time.deltaTime;

        SkelAnim.state.SetAnimation(0,walk,true);

        moveScript.WalkSpeed = 1f;
    }
    if (other.gameObject.CompareTag("WaterBomb"))
    {
        SkelAnim.state.SetAnimation(0,stand,true);

        moveScript.WalkSpeed = 0f;

        TakeDamgeEnemy (50f);
    }else{
        electrickcuted -= Time.deltaTime;

        SkelAnim.state.SetAnimation(0,walk,true);

        moveScript.WalkSpeed = 1f;
    }
}
} // this char does not fit with this code

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

Quote:
I have Debug.Log("hit by bullet"); and Debug.Log("hit by water"); thats working ,

Debugging is much more than a log file.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
v2
Comments
Member 14135033 30-Jan-19 13:17pm
   
Thank you for your help, I will get on with the above information you have provided, and see how I get on. I will repost the code now with syntax
Member 14135033 30-Jan-19 13:20pm
   
I removed the Char like you said and it says its expected with an error.
Patrice T 30-Jan-19 13:29pm
   
char can be needed because of code you didn't shown.
Member 14135033 30-Jan-19 13:34pm
   
ahh I see, yes I didnt want to paste all the code, I thought that would annoy people, just seems so strange that it works, for each tower independently, but not both together. I will set a breakpoint
Member 14135033 30-Jan-19 15:56pm
   
when I indent the code and highlight syntax, I post it here in the forum, and it all goes messed up indenting
Patrice T 30-Jan-19 16:07pm
   
See if your editor can replace tabs by spaces before pasting code here, it prevent messing.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Do you realize what is set by the first if block (the "bullet" one) is then immediately overwritten by the second one (the "WaterBomb")?
   
v2
Comments
Member 14135033 30-Jan-19 15:47pm
   
Just had a rest, ate some food, sat in my mind for thinking about it, and it just dawned on me, I just tested it and you messaged me at the same time :)

void OnTriggerEnter2D (Collider2D other)
{



if (other.gameObject.CompareTag("bullet"))
{

Debug.Log("hit by bullet");
SkelAnim.state.SetAnimation(0,electrick,true);

moveScript.WalkSpeed = 0f;

TakeDamgeEnemy (15f);

}


else if (other.gameObject.CompareTag("WaterBomb"))
{
Debug.Log("hit by water");
SkelAnim.state.SetAnimation(0,stand,true);

moveScript.WalkSpeed = 0f;

TakeDamgeEnemy (50f);



}else{

electrickcuted -= Time.deltaTime;

SkelAnim.state.SetAnimation(0,walk,true);

moveScript.WalkSpeed = 1f;

}
Member 14135033 30-Jan-19 15:50pm
   
so if hit buy bullet // do this Or if hit by waterbullet do this// ELSE walk
CPallini 30-Jan-19 16:38pm
   
Well done, I am glad you spot yourself the problem. :thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100