I need Function to be in other script
I have 2 scripts that both use attack range, 1 has the attack function and the other is the animator that uses the function. But the current attack range it is using is in the animator script and not what is in the boss weapon script. public int attackDamage = 20; public int enragedAttackDamage = 40; public Vector3 attackOffset; public float attackRange = 1f; public LayerMask attackMask; public void Attack() { Vector3 pos = transform.position; pos += transform.right * attackOffset.x; pos += transform.up * attackOffset.y; Collider2D colInfo = Physics2D.OverlapCircle(pos, attackRange, attackMask); if (colInfo != null) { colInfo.GetComponent<PlayerHealth>().TakeDamage(attackDamage); } }
That is part of the boss weapon script that contains the attack and the attack range, and I want to use this attack range in the animation script. public float attackRange = 3f; public float speed = 2.5f;
Transform player;
Rigidbody2D rb;
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
player = GameObject.FindGameObjectWithTag("Player").transform;
rb = animator.GetComponent<Rigidbody2D>();
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
Vector2 target = new Vector2(player.position.x, rb.position.y);
Vector2 newPos = Vector2.MoveTowards(rb.position, target, speed * Time.fixedDeltaTime);
rb.MovePosition(newPos);
if (Vector2.Distance(player.position, rb.position) <= attackRange)
{
animator.SetTrigger("Attack");
}
}
please help,
Answer by Kadjai · Jun 15, 2020 at 04:56 PM
Not sure exactly what you need, but you can always use the variable set on a different class if its static:
FirstScript:
public static float attackSpeed = 1f;
SecondScript
float attackSpeed = FirstScript.attackSpeed;
If you don't want it to be static, then you just write a function in your first script:
public float GetAttackSpeed()
{
return attackSpeed;
}
Then call this in the second script
SecondScript secondScript = FindObjectOfType <SecondScript>();
float attackSpeed = secondScript.GetAttackSpeed();
I am sorry I realize I was not really clear... Basically I want the attack range from the boss weapon script to be used in the animation script/second script
Attack range is... a variable? a function? Either way you should be able to call it via the 2nd half of my original post, yes?
I am getting an error when I do do it that way, and I am guessing it is because you never mention the first script in the second script?