I have set up a score system along with a multiplier which starts at 1.00 and increases in 0.25 increments, however if the player character takes damage I would like the multiplier to reset to 1.00, but for some reason I can not get this working.
The scoring is done in my GameController script, in which I simply have:
public float multiValue;
public float startingMulti = 1.00f;
void Start()
{
multiValue = startingMulti;
}
Then I have a script attached to enemies which reduces the players damage upon a collision, so I figured here would be the best place to put the multiplier reset code. After the damage has been applied I put:
What game object is the GameController script attached to? I presume there’s only one, and it’s not on any of the enemies, right? Because in your enemy scripts, when you say GetComponent(), you’re implicitly saying __this.__GetComponent(). That is, you’re asking for the first instance of a GameController script attached to the current (enemy) game object. But there is none, so the function probably returns null, and you probably have a bunch of null reference exceptions in your console window.
To do what you want, you need to somehow get access to the game object that has the GameController script attached to it. You can do this in a number of ways. You can search the entire scene for the object by name, which is easy to implement and probably good enough get past the immediate hurdle, but potentially slow in a complex scene, and not considered best practice. You could have every enemy contain a GameController field which gets initialized whenever the enemy is created; whatever creates the enemy needs to also know about the GameController instance, so that it can assign it to the enemy. If all enemies have already been created in the editor, rather than at run time, then you can just have a public GameController field on the enemy script and drag-drop the reference in the editor.
If you use a technique such as GameObject.Find(), can then call GetComponent() on that object and then set the multiValue field. If you use one of the other techniques, you should already have direct access to the GameController itself, and shouldn’t need to call GetComponent.
Ah yes, i’ve actually used the GameObject.Find() for the game controller in another script, I cant believe I forgot about that Thanks, hopefully this will sort the issue out.