Expanding the 'Roll a Ball' tutorial help for Unity 3D

Hey,

So I’ve finished the ‘Roll a Ball’ tutorial and I’m trying to implement two new cubes: ‘Bonus Cube’ and ‘Bad Cube’.

Basically, I want the ‘Bad Cube’ to Deactivate the Player Ball (And the Cube) and display a message along the lines of “You Lose”, and the ‘Bonus Cube’ to just display a message under the ball count saying something along the lines of ‘Bonus Cube Collected!’

I’ve set up the new cubes as prefabs, and all the text etc works when I interact with the basic ‘Pick Up’ cube as a test, but my issue lies with the OnTriggerEnter(Collider other) function.

I’ve made a kind of pseudo-addition to the original code to show you what I’m trying to accomplish (Right now rolling into the Bad Cube/Bonus Cube does nothing, you just phase through it)

void OnTriggerEnter(Collider other)
    { 
        if (other.gameObject.CompareTag ("Pick Up")) { //If the tag is the same as the string value...
            other.gameObject.SetActive (false); //... The game object will be deactivated.
            count = count + 1; //This changes out current value of count by +1.
            SetCountText (); //This calls the SetCountText function. 
       
            if (other.gameObject.CompareTag ("Player")) {
                other.gameObject.SetActive (false);
                loseText.text = "YOU DIED";
            }

            if (other.gameObject.CompareTag ("Bonus Cube")) {
                other.gameObject.SetActive (false);
                bonusText.text = "Bonus Cube Collected!";
            }

        }

Any help will be appreciated! Cheers.

1 Like

Check your logic here… the first thing you do is check whether the other tag is “Pick Up” on line 4. If not, you jump to the end of that if-block, on line 19.

If it is, then you increase your count, and go on to check whether the tag is “Player” or “Bonus Cube” …which of course it’s not, because we’ve already established at this point that the tag is “Pick Up”.

2 Likes

So would I have to create a new function for each trigger? For example

void OnTriggerDie (Collider col)
    {
        if(col.gameObject.CompareTag ("Player")) 
        {
            col.gameObject.SetActive (false);
        }
    }

No, just fix your logic. You want to see if it’s a player. THEN, after the close of that “if” block, you want to see if it’s something else.

If you don’t understand the syntax & meaning of if blocks (or blocks of code in general), then spend a bit of time working through these tutorials, and I bet it’ll all become clear. This might help too.

1 Like

,
Right, it works now thanks!

That was actually the first thing I attempted with no result earlier, I might have overlooked a syntax or tag error.

1 Like