- Home /
How do I call a funtion without refrencing the script?
Im working on a combat system for my game. I plan on having any damage a player would take being done via a function inside the players health script. What I would like to know is there a way to call this function without having to reference the playerscript inside every enemy and object that would hurt the player?
Why not have the function merely happen on collision from within your player script? Just setup an OnTriggerEnter function on the Player that contains checks for what type of object entered, so you can apply your damage types. Here's an example in C# (Comment back if you can't convert it to JS yourself):
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Enemy1")
{
playerStat.hp -= 10
{
else if (other.gameObject.tag == "Enemy2")
{
playerStat.hp -= 14
{
//and so on and so on etc....
}
Then just set the proper tags on each of your enemy, projectile, weapon, etc.. prefabs.
Because I need it to be more flexible than that. If I have 100 enemy's that each do a different amount of damage I don't want to make 100 different tags.
Answer by iwaldrop · Dec 24, 2013 at 05:15 AM
You could use Unity's SendMessage. All you have to know then is the GameObject that you're trying to send the message to. It would look something like this:
void OnCollisionEnter(Collision collision)
{
collider.gameObject.SendMessage("DoDamage", 5, SendMessageOptions.DontRequireReceiver);
}
Additionally, you could get the component during a collision.
void OnCollisionEnter(Collision collision)
{
Player player = collision.gameObject.GetComponent<Player>();
if (player != null)
player.DoDamage(5);
}
There's nothing wrong with either one, but I think GetComponent is slightly faster.
Think I will go with the collision opttion. I still think there should be an easier way but eh this way should do.
I don't know what you mean by 'easier'. Perhaps you did not provide enough information in your question. You did not describe the physical interaction between player and enemy, so I assumed collisions. If you would like, because you've already accepted this one as answered, ask another, more detailed, question which provides some qualifiers for a solution.
It is collisions. Weapon/projectile to player. What I was wondering is if I could make the script into a different type of file that allowed its functions to be available to every script in the scene or something along those lines.
If you want it to be accessible in the way you mention, then you could use Class Inheritance on the script which contains the Trigger statement. Ins$$anonymous$$d of having "EnemyHurt : $$anonymous$$onodevelop" at the top of your script, it would be "EnemyHurt : Playerwhateverscript".
Here's a good resource that explains it: http://csharp-station.com/Tutorial/CSharp/Lesson08
(although I don't know if class inheritance exists in Javascript)
You don't really need to. You just need a single $$anonymous$$onoBehaviour that deals damage to the player. It would be placed on all enemies. If you wanted to define how much damage the enemy did inside of the Enemy script ins$$anonymous$$d of the DamageDealer script, simply have the Enemy object set the DamageDealer's damageValue.
In fact, this solution could be more performant because that script could be on a sub-GameObject of the enemy that only interacts with the Player's layer. In this way you can change the collider type to a trigger, which causes Unity to generate less information than a real physics collision.
Your answer
Follow this Question
Related Questions
How to use functions between two scripts 2 Answers
OnCollisionEnter does not work. 1 Answer
Restart this script 3 Answers
Camera Move 1 Answer
How to call a function from a script in another scene 5 Answers