Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can somebody PLEASE help me with this dam button?

Discussion in 'UGUI & TextMesh Pro' started by Durins-Bane, Mar 24, 2015.

  1. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    Deleted
     
    Last edited: Apr 25, 2015
  2. Robin-Jubber

    Robin-Jubber

    Joined:
    Sep 7, 2012
    Posts:
    28
    I'm not sure I entirely follow your problem but you could do it by adding an Event Trigger to the Button. That allows you to add all sorts of triggers, for instance when the mouse enters the Button. Perhaps you could hack it so that On Enter stores the string in a global, whilst On Click calls the function.

    Or perhaps make it so that the On Click function just sends the gameObject ID of the Button itself. Then the function that is called could look for the Text component of the Button it just got sent, and read the data out of it?

    One other less hacky approach is for your UI Manager script, or wherever you deal with all of this, to have an array of possible buttons as a public var (eg public Button[] all_my_buttons) which you laboriously fill with all the buttons you might need to interrogate. Then, say you have 5 buttons the player might click, make each one send an On Click function with that number, corresponding to the array. Then your function can interrogate the Button in the array of Buttons when it receives the number. FindComponents (or FindComponentsInChildren), looking for the Text field, and bob's your uncle.

    Hope this explains one route
     
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Just combine the string and set the text directly.
     
  4. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    I have this here but my leaderboard is in CS and I only really know JS

    I use this to get the score

    Code (JavaScript):
    1. if(dead){
    2. disableMouseLook();
    3. score = bulletTraceGO.GetComponent("bulletTraceGenerator").score;
    4. }
    and this to supposedly add it to my leaderboard which does work when I use the Highscores CS script

    Code (JavaScript):
    1. function addnew(name:String){
    2. GetComponent("Highscores") AddNewHighscore(name, score);
    3. Debug.Log(name + ": " + score);
    4. }
    I use this to update the score when the player dies I attach this to the UI text gameobject that dispalys the score

    Code (JavaScript):
    1.  var GameSettingsOB : GameObject;
    2. var playersScore : int = 0; //players score
    3. var playerScoreGO: GameObject; //the texts gameobject which displays the score when player dies
    4. var bulletTraceGO : GameObject; // holds the players actual score
    5. function Start()
    6. {
    7.   GameSettingsOB = GameObject.FindWithTag("Settings");
    8.   playerScoreGO = GameObject.FindWithTag("score");
    9.   bulletTraceGO = GameObject.FindWithTag("BulletTrace");
    10. }
    11. function Update()
    12. {
    13.      if(GameSettingsOB.GetComponent("Game Settings").dead == true){             //is the player dead?
    14.       playersScore = bulletTraceGO.GetComponent("bulletTraceGenerator").score;
    15.       playerScoreGO.GetComponent(Text).text = playersScore.ToString();        //set the score
    16.      }
    17. }
    18.  

    The debug for the above shows the name and score but it seems like

    Code (CSharp):
    1. public void AddNewHighscore(string username, int score){
    2.         Debug.Log ("Adding new highscore");
    3.         StartCoroutine (UploadNewHighscore (username, score));
    4.     }
    this never gets called as this debug doesnt show up and there are no errors


    edit: I now get this error
    NullReferenceException: Object reference not set to an instance of an object

    on this line
    Code (JavaScript):
    1. instance.StartCoroutine (instance.UploadNewHighscore (username, score));
     
    Last edited: Mar 24, 2015
  5. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    Last edited: Mar 24, 2015