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

Asked

Viewed 47 times

2

Hello, I’m trying to make a game but I found a problem where I can’t solve, maybe the answer is really stupid, but since I’m kind of busy, I can’t 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 contacts the "Enemy" he disappears as he should, but the "[R] Restart" button does not, since he is from another class. I’ve tried to do it in a few ways, but I’m really tied up and I don’t really remember what they were. The idea here is that the player dies, appears the text of "Restart" and after pressing the R key he returns to the position of "spawnPoint", which would be the starting position and would count "+1" in the text of "Retries". But for some reason when Player dies, it seems to stop working, so I tried to create another script to handle these situations, without the same stops working.

I wonder if I can follow this logic, or if I have to do otherwise.

  • I haven’t touched unity3d in a couple of years, so I don’t really remember, but the impression I get is that if the two scripts are in the same object and depend on each other, then it would be easier if they were one script. And maybe that rb.gameObject.SetActive(false); in the method Die() go disable the object acting on both scripts.

  • The first script is connected directly to the "Player" object, the second script is in a separate object. Rb.gameObject.Setactive(false); in the Die() method it is to disable the Player, so that it cannot move. I tried in other ways, like this.gameObject.Setactive(false); or even calling from the other class, but it always "hangs"

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.