Finished Section 5, expanding my game, need help


So I’ve finished the LaserDefender section, and am working on expanding my game.

The first thing I’m working on is adding powerups. Using the scripting we used to generate the laser beams, when the enemy dies, it now randomly drops a powerup. Right now, I have 2 working powerups, Shields, and Shields+, they both add to the players health.

The problem I am having is with doing a weapon powerup.

I need to find out how to change the damage the projectiles my ship fires on the fly.

Is there a way to change the damage variable in Projectile.cs when, and only when, my ship fires a projectile?
Or would I have to create public variables in both player and enemy, and modify the Projectile.cs so it calls those variables instead of using its own variable? If that is the case, how would I tell whether Player or Enemy is doing the shooting?


Nevermind. I figured it out.

For those that are interested:

In PlayerController.cs and Enemy.cs (or whatever your enemy controller script is)
Create a public static float variable called firepower, set it to whatever your default firepower is, mines 100f.

Then in your Fire() function, add the following line to the end of the function:
beam.SendMessage(“setdamage”, firepower);

Then in your projectile.cs script, add the following function:
public void setdamage(float firepower) {
damage = firepower;

Now, I’m sure there is probably a better way of doing it, but that’s what I found, and it’s working for me.

So, answered my own question lol. I think having to formulate my problem into a coherent question helped me figure it out. That and a little googling.


Could you possibly have an on trigger action in your player controller script that activates when an object with a tag (i.e. firepowerboost) is set to run a code that increases the firepower for a certain duration using a timer.

that way firepower can be varied depending on the power up and only for a limited time as you can have a number of different tags each time the timer runs out it resets the firepower to the base level.

like you say to your result it may not be the best method but adds the functionality to be able to add stuff later without adjusting code every time.


That does sound like a better way. I’ll have to look into the use of tags.

My next step was going to be timers. Right now, if you get a powerup, it stays with you till you die. Now to look up timers when I get home.