Jump to content

I need C#/Unity assistance

CreepyPL

I am making a simple mobile game and am decently new to programing, and i try to figure this problem for literally about 2 weeks, and everytime i get back to this problem i face the same thing that i dot understand and doesnt make any sense at all in my opinion. Heres the code:
 

private void OnCollisionEnter2D(Collision2D collision)
    {
        if (tag == "Breakable")
        {
            NormalBlock();
            creepyCoin.AddCCAfterHit();
        }
        else if (tag == "Bomb Block")
        {
            BombBlock();
        }
    }

    private void NormalBlock()
    {
        blockHP--;
        if (blockHP <= 0)
        {
            Destroy(gameObject);
            level.BlockDestroyed();
        }
        blockHPText.text = blockHP.ToString();
    }

    private void BombBlock()
    {
        GameObject[] allNormalBlocks = GameObject.FindGameObjectsWithTag("Breakable");
        foreach (GameObject obj in allNormalBlocks)
        {
            NormalBlock();
        }
        Destroy(gameObject);
    }
}

The exact problem i have is that the "NormalBlock" method in the "BombBlock" method doesnt get executed for some reason, it really gives me headache since it makes no sense that it does not work (to me).

Link to comment
Share on other sites

Link to post
Share on other sites

27 minutes ago, CreepyPL said:

The exact problem i have is that the "NormalBlock" method in the "BombBlock" method doesnt get executed for some reason, it really gives me headache since it makes no sense that it does not work (to me).

If the NomalBlock() does not trigger inside BombBlock() it's because of 2 reasons only (really the only 2 reasons anything else you might think it is not) :

1 ) You are not calling BombBlock()

2 ) Your allNormalBlocks collection contain 0 elements

Link to comment
Share on other sites

Link to post
Share on other sites

Consider using Debug.Log("some kind of debug message"); in your code to see what lines get executed and what does not.

Check if the onCollisonEnter method gets called, then check what path your code takes. You could do the following for the BombBlock method:

 private void BombBlock()
    {
        GameObject[] allNormalBlocks = GameObject.FindGameObjectsWithTag("Breakable");
        Debug.Log(allNormalBlocks.Length);
        foreach (GameObject obj in allNormalBlocks)
        {
        	Debug.Log("NormalBlock in BombBlock is getting called!");
            NormalBlock();
        }
        Destroy(gameObject);
    }

 

Link to comment
Share on other sites

Link to post
Share on other sites

You didn’t tell which block you want to lose HP. 

 

Additionally your all normal block data structure could be empty. Serialise the field & see what happens when you press play. 

Link to comment
Share on other sites

Link to post
Share on other sites

Do those if statements in the OnCollisionEnter2D even get triggered? (as others suggested, try a Debug.Log)

I'm pretty sure you need to do it something like this:

void OnCollisionEnter2D(Collider2D collider)
{
	if (collider.gameObject.tag == "Breakable")
	{
		// Do a thing with Breakable.
		Debug.Log("The code for breakable is now running - This Debug.Log shows that it is working.");
	}
}

Specifically talking about the collider.gameObject.tag part.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Put some Debug.Log()s in your code. At the start of each method, inside loops and if blocks. Spit out the values of the variables concerned. This will give you a better picture of what's going on.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×