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. Dismiss Notice

String copied to button dissapearing (?!)

Discussion in 'Scripting' started by blober81, Oct 16, 2016.

  1. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Hi everyone,

    When I copy a string variable to a button script (SetReference) it passes the name like it should. But when I click on the button the reference is ""

    Does anyone have an idea what could went wrong here ?

    Thanks in advance.


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. //usingUnityEngine.UI;
    6.  
    7. public class TButtonReact:MonoBehaviour{
    8.  
    9. public string BtnName;
    10. public GameObject Tdummy;
    11. bool oneClick=true;
    12.  
    13. public string reference;
    14.  
    15.  
    16. void Start()
    17. {
    18. BtnName=gameObject.name;
    19. }
    20.  
    21.  
    22. public void onClick(){
    23.  
    24. if(oneClick)
    25. {
    26. oneClick=false;
    27. Debug.Log("buttonreference "+ reference);
    28. Tdummy.GetComponent<ScrollBO>().SelectedObject (reference); //<-- (2) but here reference is ""
    29. }
    30. }
    31.  
    32.  
    33. publicvoidOnMouseUp()
    34. {
    35. oneClick=true;
    36. }
    37.  
    38.  
    39.  
    40. public void SetReference (string name)
    41. {
    42. reference = name; //<--(1) here reference is "red"
    43. Debug.Log("buttonreference "+ reference);
    44. }
    45.  
    46. }
    47.  
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Try changing "reference" to be private instead of public, to rule out outside interference with the value. If you need it to be displayed in the inspector to set the default value there, use the [SerializeField] attribute above it. Be sure that you don't have multiple copies of this script on the GameObject either.

    For future reference, please copy the script into the Insert->Code window directly, rather than just pasting the code in the textbox and then putting [code ][/code ] around it, because that doesn't maintain the whitespace properly.
     
  3. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Like this ? Because I still get the same error that reference = "".
    To clearify things; the reference variable is the button text value.

    Code (CSharp):
    1.     [SerializeField]
    2.     private string reference;
     
    Last edited: Oct 16, 2016
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Where are you calling setReference from?
     
  5. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    An empty gameobject. The string is passed by setReference because when I do....

    Code (CSharp):
    1. reference = name;
    ...the reference is named "red" thus so far so good. However when I excecute onClick method the reference all of a sudden is "".
     
    Last edited: Oct 16, 2016
  6. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    From another script thats placed on an empty gameobject.

    Code (CSharp):
    1. GameObject.Find (newButtonBO.name).GetComponent<TButtonReact> ().SetReference(TmenuLoad[i-1]);
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Whatever newButtonBO.name references, are you sure that is targetting the correct button that you are also later clicking on?
     
  8. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Yes, just double checked. Another thing. The first time I load the buttons the reference name is passed to the buttons like it should and everything works perfect. When I replace the buttons with different buttons all of a sudden the reference is "" on the buttons.

    As the reference string is equal the button's text I might get the buttons text by code within the button ?

    Here is the code that loads the button list.

    Code (CSharp):
    1.     void LoadList()
    2.     {
    3.  
    4.         //voeg buttons toe aan queque list
    5.         for (int i = 1; i <= TmenuLoad.Count; i++) {
    6.  
    7.             GameObject newButtonBO = Instantiate(buttonTop) as GameObject;
    8.             newButtonBO.transform.SetParent(this.transform, false);
    9.             posY = centerPan;
    10.             posX = guiVecL.x + (btnW * i);
    11.             setPos = new Vector2 (posX, posY);
    12.             newButtonBO.transform.position = setPos;
    13.             newButtonBO.transform.SetParent (this.gameObject.transform);
    14.             newButtonBO.name = "ButtonBO" + i;
    15.             newButtonBO.GetComponentInChildren<Text>().text = TmenuLoad[i-1];
    16.             //lastBtn = newButtonBO.name;
    17.             BtnL.Add (newButtonBO.name); //add button list
    18.             GameObject.Find (newButtonBO.name).GetComponent<TButtonReact> ().SetReference(TmenuLoad[i-1]);
    19.         }
    20.         //list loop is klaar dus eerste en laatste buttons kunnen worden aangewezen.
    21.         SetFirstLastBtn();
    22.     }
     
  9. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    This is so confusing... Like I said when I copy the reference string value to the button it stores the string name like it should but when I click the button the reference string all of a sudden is "".

    As this doesn't work for unknown reason(s) maybe there is a work around. Can I acces the Button text value form within the button script ? I've googled for solutions but so far nothing did the trick. The reference is the same as the button text so maybe it work if I just retrieve the button's text string from within the button script itself.
     
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    You should be able to get the text in the child with (assuming it's the only child)
    Code (CSharp):
    1. string childText = transform.GetChild(0).GetComponent<Text>().text;
    For your other issue, off hand I'm not sure where something is going wrong.
     
  11. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Have you confirmed that nothing else is calling SetReference? Including extra copies of the reference-setting script without the right values filled in?
     
  12. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Thanks a ton ! the text in child option fixed it for me. Maybe later i'll find out why the original option didnt work out.

    Cheers !