Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Stack overflow problem

ok so i got a problem here. Originally i wanted to use a class for this but I don't know how to change class variable via another script. So i did this and it did what I wanted it do do ( change the player's score : https://forum.brackeys.com/discussion/331/updating-text-via-script-not-working#latest) So everything works fine expect the stackoverflow. There a 2 scripts working together so here they a seperated by "------"

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;


public class AddScore : MonoBehaviour

{

  int scorenum = 0;

  public Text score;

  public GameObject scorechanger;


  void Update()

  { 

    score.text = scorenum.ToString();

  }


  private void OnTriggerEnter(Collider other)

  {

    var c = scorechanger.GetComponent<ScoreClass>();

    if (other.gameObject.tag == "Player")

    {


      c.Changescore(scorenum);

    }

  }


  public void updatescore(int num)

  {

    scorenum = num;

  }


  public void scoreup(int no)

  {

    scorenumber = no;

    upgradescore();

  }

   

  void upgradescore()

  {

    scorenum++;

    scorechanger.GetComponent<ScoreClass>().Changescore(scorenumber);

  }



}

--------------------------------------------------------------------------------------------------------------------------------------------------------

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class ScoreClass : MonoBehaviour

{

  public GameObject scorescript;

  public int scorenum = 0;

  int scorenumber;

  private void Start()

  {

    scorescript.GetComponent<AddScore>().updatescore(scorenum);

  }


  public void Changescore(int scoreno)

  {

    scorenum = scoreno;

    scorenum++;

    returnscore();

  }


  void returnscore()

  {

    scorescript.GetComponent<AddScore>().scoreup(scorenum);

  }

}

Also it would be great if you have a video explaining how to how to change a class variable via another script(if it is possible)

Thanks for helping 😉

Best Answer

  • ThePandaGameThePandaGame Member
    Accepted Answer

    SOLVED !

    Ok so, i recreated the script to do the same thing but differently and i also made it smaller and it worked with no error

Sign In or Register to comment.