H
H
Hiniks YouTube2022-03-16 18:43:54
C++ / C#
Hiniks YouTube, 2022-03-16 18:43:54

How to call a class from another script (attached to the same object)?

THIS IS THE FIRST SCRIPT FROM IT NEED TO TAKE A CLASS (STOP)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class combatPlaye: MonoBehavior
{

public Animator anim;
public Transform AttackPoint;
public float attackRange = 0.5f;
public LayerMask enemyLayers;
public Vector2 moveVector;

public int attackDamage = 40;
public float attackRate = 2f;
float nextAttackTime = 0f;

void Update()
{
if(Time.time >= nextAttackTime)
{
if (Input.GetKeyDown(KeyCode.F))
{
Attack();
nextAttackTime = Time.time + 1f / attackRate;
}
}


}

void Attack()
{
// ATTACK ANIMATION
anim.SetTrigger("Attack");

Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(AttackPoint.position, attackRange, enemyLayers);

foreach(Collider2D enemy in hitEnemies)
{
enemy.GetComponent().TakeDamage(attackDamage);

}
}

void OnDrawGizmosSelected()
{
if (AttackPoint == null)
return;

Gizmos.DrawWireSphere(AttackPoint.position, attackRange);
}

public void Stop()
{
this.enabled = false;
}

}

THIS IS THE SECOND SCRIPT INTO IT YOU NEED TO CALL THE CLASS (STOP)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehavior
{
public Rigidbody2D rb;
public Vector2 moveVector;
public float speed = 3.5f;
public float jumpForce = 6f;
public Animator anim;
public SpriteRenderer sr;
public bool onGround;
public Transform GroundCheck;
public float checkRadius = 0.5f;
public LayerMask Ground;
public static Player Instance { get; set; }
[SerializeField] private int lives = 5;

void Start()
{
rb = GetComponent();
anim = GetComponent();
sr = GetComponent();
Instance = this;
}

void Update()
{
Walk();
Jump();
Flip();
CheckingGround();
}

//WALK

void Walk()
{
moveVector.x = Input.GetAxis("Horizontal");
anim.SetFloat("MoveX" , Mathf.Abs(moveVector.x));
rb.velocity = new Vector2(moveVector.x * speed, rb.velocity.y);
}

//JOY

void Jump()
{
if(Input.GetKeyDown(KeyCode.Space) && onGround)
{
rb.velocity = new Vector2 (rb.velocity.x, jumpForce);
}
}

void Flip()
{
if(moveVector.x > 0)
{
sr.flipX = false;
}
else if (moveVector.x < 0)
{
sr.flipX = true;
}
}

void CheckingGround()
{
onGround = Physics2D.OverlapCircle(GroundCheck.position, checkRadius, Ground);
anim.SetBool("onGround", onGround);
}

public void GetDamage()
{
lives -=1;
Debug Log(lives);
}

void Stoper()
{
if(moveVector.x > 0)
{
Stop();
}

}

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
K0TlK, 2022-03-16
@hiniks

Stop is not a class, but a method. If they are attached to the same object, then you can get the component via GetComponent, just like you do with a rigidbody. In your case, this is
GetComponent<combatPlaye>(), and then call the method you need on the cached component.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question