How to make a customizable variable in this script?
How do I make the damage to the player a customizable script as I want all melee enemies to use the same script but with different damage and it would take too much resources to duplicate and lightly modify the script for each enemy type.
Here is the script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Mushroom_attack_script : MonoBehaviour { public Sprite sprite_attack; public SpriteRenderer spriteRenderer; public Sprite sprite_idle;
void OnTriggerStay(Collider other)
{
//It will check for the name of the GameObject that had enter inside the enemy trigger
if (other.gameObject.name == "FPSController")
{
HealthBarScript.health -= 0.0075f;
}
} void OnTriggerEnter(Collider other) { //It will check for the name of the GameObject that had enter inside the enemy trigger if (other.gameObject.name == "FPSController") { spriteRenderer.sprite = sprite_attack;// Change sprite }
}
void OnTriggerExit(Collider other)
{
//It will check for the name of the GameObject that had enter inside the enemy trigger
if (other.gameObject.name == "FPSController")
{
spriteRenderer.sprite = sprite_idle;// Change sprite
}
}
}
Answer by Vega4Life · Dec 25, 2018 at 10:07 PM
Is this what you are looking for?
using UnityEngine;
public class Mushroom_attack_script : MonoBehaviour
{
public float damageAmount = 0.0075f; // Set this in the inspector for each melee guy
public Sprite sprite_attack;
public SpriteRenderer spriteRenderer;
public Sprite sprite_idle;
void OnTriggerStay(Collider other)
{
//It will check for the name of the GameObject that had enter inside the enemy trigger
if (other.gameObject.name == "FPSController")
{
HealthBarScript.health -= damageAmount; // Take out the literal and put in our variable.
}
}
}
When you attach your script to each enemy, you go into the inspector and set the amount of damage you want each one to do in the Damage Amount variable.
Thank you so much for helping as I couldnt find the answer anywhere else for hours.