How to use functions of class A in class B and vice versa?

2

Hello, I'm trying to make a game but I found a problem where I can not solve it, maybe the answer is pretty stupid, but since I'm a bit busy, I can not find it at all.

This is my first class or Script, which controls the player

public class PlayerController : MonoBehaviour {

public int speed;
public int score;

public bool isGrounded;
public bool isWalled;
public bool isDead;

public Text scoreText;

public Transform spawnPoint;

public Vector3 jumpHeight = new Vector3(0, 5, 0);
public GameController gameScript;

Rigidbody rb;
Transform playerTrn;


public void Start () {

    isDead = false;
    isWalled = false;
    isGrounded = true;

    GameController gameScript = GetComponent<GameController>();
    playerTrn = GetComponent<Transform>();
    rb = GetComponent<Rigidbody>();

    score = 0;
    SetScoreText();

}

private void FixedUpdate () {

    if (Input.GetKey(KeyCode.D))
        playerTrn.Translate(new Vector3(speed, 0, 0) * Time.deltaTime);

    if (Input.GetKey(KeyCode.A))
        playerTrn.Translate(new Vector3(-speed, 0, 0) * Time.deltaTime);

    if (isGrounded && Input.GetKey(KeyCode.Space))
    {
       rb.velocity = jumpHeight;
       isGrounded = false;
    }

    if (isDead == true)
    {
        gameScript.Restart();

        if (Input.GetKey(KeyCode.R))
        {
            isDead = false;

            Instantiate(playerTrn, spawnPoint.position, spawnPoint.rotation);
        }
    }

}

public void OnCollisionEnter(Collision other)
{
    if (other.gameObject.CompareTag("Ground"))
    {
        isGrounded = true;
    }
    if (other.gameObject.CompareTag("Enemy"))
    {
        Die();
    }


}

public void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Collector"))
    {
        score = score + 1;
        SetScoreText();
        other.gameObject.SetActive(false);
    }
}

void SetScoreText()
{
    scoreText.text = "Score: " + score.ToString();
}

public void Die()
{
    if (isDead == false)
    {
        isDead = true;
        rb.gameObject.SetActive(false);
    }

    return;

}

And this is the GameController class, which should control the outside of the player.

public class GameController : MonoBehaviour {

public GameObject playerObject;
private PlayerController playerController;

public int retries;

public Text restartText;
public Text retriesText;

public void Start()
{
    playerController = GetComponent<PlayerController>();
    retries = 0;
    SetRetries();
}


public void SetRetries()
{
    retriesText.text = "Retries: " + retries.ToString();
}

public void Restart()
{
    restartText.text = "Restart [R]";
}

Currently, the moment the player comes into contact with "Enemy" it disappears as it should, but the "[R] restart" button does not, since it is of another class. I've tried to do it in a few ways, but I'm too wrapped up and I can not remember what they were. The idea here is for the player to die, the text for "Restart" appears and after pressing the R key it returns to the "spawnPoint" position, which would be the starting position and would count "+1" in the Retries text. But for some reason when the Player dies, it seems to stop working, so I tried to create another script to handle those situations, without it even stopping.

I wonder if I can follow this logic, or if I have to do it any other way.

    
asked by anonymous 18.06.2018 / 02:13

0 answers