About 'Introducing Encapsulation'!

In this video (objectives)…

  1. Ensure our Canvas Scaler is set correctly.
  2. Use FindObjectOfType() to find the scoreBoard.
  3. Communicate between enemy and scoreboard.

After watching (learning outcomes)…

Separate a script into two, and arrange for them to find and and communicate with one another in a basic way.

(Unique Video Reference: 31_AA_CU2)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

I’m getting an interesting side effect of having two lasers where I’m guessing they’re both hitting the object in the same frame and causing 24 points to be added instead of 12!!

2 Likes

Hello Cam,

I am getting the same results as you are. I fixed this by doing the following.

Created a private class field:

private bool canHit = true;

Then I created a private method to implement code to wait 2 seconds. This function will set canHit to true and wait.

private IEnumerator Wait()
    {
        canHit = true;
        yield return new WaitForSeconds(2);
    }

Finally in the OnParticleCollision method I surrounded my current code with an if statement and changed the canHit to false to stop the code from processing. After my if block, I called the wait function.

void OnParticleCollision(GameObject other)
    {
        if (canHit)
        {
            canHit = false;
            GameObject fx = Instantiate(deathFx, transform.position, Quaternion.identity);
            fx.transform.parent = parent;
            Destroy(gameObject);
            scoreBoard.ScoreHit(scorePerHit);
        }

        Wait();
    }

There is some new stuff here. Here is a link to the unity documentation:

https://docs.unity3d.com/ScriptReference/WaitForSeconds.html

I am sure there is more elegant way to do this, but this is working for me for now :slight_smile:

@ben may address this later in the series. I am just finishing this video and figured I give it a go to try to fix.

Good luck!

Michael

1 Like

Thanks, Michael. Nice work. Glad I’m not the only one getting this. I’m curious to see if this gets dealt with later too.

Cam,

The very next video deals with an enemy health system, so it actually makes sense to have multiple hits :slight_smile:!

It was worth going through the exercise though as I am sure this will come up in my future game design and now I have some code and research to solve it. Being on the leading edge of the content release keeps us all in suspense for whats next, lol. It has been fun though as I have been creating branches exploring what I could do next and seeing if that is the way that Ben and Rick go also.

Michael

Gah! They must’ve come out just as I went to bed last night after hitting the last available video!

Privacy & Terms