Search Unity

Why a value from a script attached to prefab cannot be passed to another script?

Discussion in 'Scripting' started by zyonneo, Jan 8, 2020.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Code (CSharp):
    1.  
    2. if (HomeScreen.HomeScreenInstance.Deletevalue == true)
    3. {
    4.              
    5.   go.transform.GetChild(1).GetComponent<Button>().gameObject.SetActive(true);
    6.   string buttontxt = go.transform.GetChild(0).GetComponent<Text>().text;
    7.  
    8.  //OnClick added to the button via code
    9.  go.transform.GetChild(1).GetComponent<Button>().onClick.AddListener(() =>   DeleteGivenNote(buttontxt));
    10.  
    11.  }
    12.  else
    13.    {
    14.     Debug.Log("Delete Value..... " + HomeScreen.HomeScreenInstance.Deletevalue);
    15.    }
    16.  
    17.  
    18. public void DeleteGivenNote(string btntxt)
    19. {
    20.  
    21.   string foldername = LoaditemsDropdown.options[LoaditemsDropdown.value].text;
    22.  
    23.   int btnindex = ButtonDetailsClick.ClickInstance.buttonIndex;
    24.   Debug.Log("Button Index and btn text = " + btnindex+" :: "+btntxt);
    25.   if (File.Exists(Application.persistentDataPath + "/" + foldername +"/"+btntxt+ ".json"))
    26.     {
    27.      
    28.       infos.Remove(infos[btnindex]);
    29.       // File.Delete(Application.persistentDataPath + "/" + foldername + "/" + btntxt + ".json");
    30.       // Load();
    31.       for (int i = 0; i < infos.Count; i++)
    32.        {
    33.           Debug.Log("infos after" + infos[i]);
    34.        }
    35.     }
    36.  
    37. }
    38.  

    The below I added in the prefab and attached it to the prefab button Onclick.Dragged and dropped the DeleteIndex() method to the button.
    Code (CSharp):
    1. [HideInInspector]
    2.     public int buttonIndex=0;
    3.  
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.         ClickInstance = this;
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.    
    15.     }
    16.    
    17.     public void DeleteIndex()
    18.     {
    19.         buttonIndex = this.transform.GetSiblingIndex();
    20.         Debug.Log("Indexxx = " + buttonIndex);
    21.     }
    22.  
    Even though the Indexxx value gets printed it is giving 0 value in the DeleteGivenNote method.Is this because I have given both methods to the same button.One Via code and other dragged and dropped.How to get the Sibling Index of a prefab it it is instantiated under a transfrom many times.
     
    Last edited: Jan 8, 2020