#Applying Damage
1 messages · Page 1 of 1 (latest)
simplest way would be by doing
var gameplayPlayers = FindObjectsOfType<GameplayPlayer>();
foreach (var player in gameplayPlayers)
{
player.Damage(20);
}
alternatively you could track down the location of the player in the hierarchy (it would always be the same) and cache it from there if you'll be using the player a lot
I want to code a special object that will damage the player if they come in contact with it that will spawn at a particular time during the battle.
This sounds just like a note right? Except maybe you don't want it to move down the lane? In that case you could also just give the special object some of the note components and it would handle damage for you.
Otherwise calling the player damage method would work just fine too
Thanks for the help so far yall! When i try to use gameplayPlayer, my vs code can see the class just fine but when i import the code into unity via my dll and then try to compile the mod, it errors out and says that it cannot find that class. I also tried attaching the components of a note prefab to my object but when it collides with the player, and i confirmed that it does indeed collide, it does not affect the players health.
Do you want to share the code? There are a few weird reasons for code not compiling that you can normally work around.
Also, for the components solution - the components added to the note in unity is not the same as the components the notes in game actually have added to them. I can show you which class I mean later
man this spambot really hates me pasting code
sorry for the delay, your code looks reasonable, having trouble seeing any particular issue! The only thing that I haven't seen before is using FindObjectsOfType - I'd normally find the active player like this:
you get a picture because the bot hates code
maybe a silly thing to ask but it might be worth restarting unity if you've just updated your dll, this can also fix compilation issues.
Tried that, didn’t do anything. Unity has annoyingly been unable to see any of the games classes the whole time I’ve been working on this project
It’s okay! I really appreciate the help!
super silly question - are you adding the c# scripts directly into your project or building a DLL?
I accidentally did both at first but now I’m just adding the compiled DLL file into the project
and just to double check, you've been able to compile with a dll and some custom scripts, its just adding this particular script that caused issues?
Whenever I make calls to any of the games classes is when I run into trouble
VS Code has no problem making those calls and I don’t get any errors in unity either sometimes until I compile the mod
is your DLL in your Mod Folder in the unity project? I know it wouldn't build if it was in the top level folder
also out of interest are you able to expand the dll in the unity explorer window like pictured or does that cause errors too?
not 100% sure what's going on just trying to figure out if building the mod is the issue or it can't go in the unity project full stop
It’s in the mod folder
That does not cause any issues
weird, this is not something I've personally experienced before. Not that I think this is the correct way around this but this solution should definitely work:
If you add a GameplayPlayer.cs file into the unity project and include a basic scaffold for the GameplayPlayer class (Just inherit it from MonoBehaviour, define a Damage function that takes in an integer does nothing), then your project should be able to compile.
^ Doing this allowed me to even include game classes as serializable fields of my custom scripts, which normally produces the same errors you are getting.
Managed to fix it! I noticed on the custom script mod tutorial page there was this snippet that ive attached. I followed that and was able to make my code work without having to include a scaffold and better yet, the intended damage functionality works in game! I appreciate the help, thank you!
Oh, was there an object you were storing that I missed? Sorry I would've sent you to this snippet if I had seen that :(
But yeah what I outline above is essentially a workaround to that snippet, which is really only useful if you want to instantiate these in game classes rather than grab them by reference.
Its okay, i truly appreciated your help!