- Home /
How to get to pass multiple informations through button click ?
Hello everyone, My problem is I guess pretty simple but I'm still a beginner with Unity, and although I've searched everywhere I can't seem to find how to do exactly what I want. I have a button that everytime when clicked on, instantiates a new character sheet with random features (name, experience, etc) and puts it under the previous one so you get a list of characters. When clicking on the "managing" button on a character sheet, you should have access to another sheet where the character informations are written. So, I've made a managing sheet template and I can't find how to put the good informations on it depending what character was clicked. I thought of making an array or something but I need the informations to be able to change, like when you gain experience etc. Thank you !
I'm presu$$anonymous$$g that you have a "CharacterSheet" script or somesuch attached to each character sheet, that fills in the specific character's information on each instance. So that should know what character it relates to. $$anonymous$$ake the click event for the "managing" button on each character sheet call a function on that sheet's CharacterSheet script. This function can then bring up the "managing sheet" and tell it which character is to be managed.
I do have a script that's attached to each of my characters and that contains each variable about them. I also have a list in which all the characters are stocked. The character scripts are on objects that are parents to the button, so my problem is that when a button is pressed, it has to access the button's parent to know the ID, but I don't really know how to do that... When the button is clicked, I have a function in my Game$$anonymous$$anager that triggers another function in my $$anonymous$$anaging$$anonymous$$anager (sorry ahah), which is used to fill the managing sheet with the good informations.
This is the script part that instantiates a character :
public void OnNouveauGroupe()
{
spawnPoint = spawnOrigin.transform.position.y;
GameObject groupe = Instantiate(groupeFiche, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
nombreQueens += 1;
Queen tmpQueen = groupe.GetComponent<Queen>();
tmpQueen.queenID = nombreQueens;
queens.Add(tmpQueen);
groupe.GetComponentInChildren<Button>().onClick.AddListener(() => Game$$anonymous$$anager1._instance.On$$anonymous$$anagingClicked());
groupe.transform.SetParent(GameObject.Find("Background_queens").transform);
groupe.transform.position = new Vector3(0, spawnPoint + (-(nombreQueens-1) * spacing), 0);
groupe.transform.localScale = new Vector3(1, 1, 1);
nouveauGroupe.transform.position = new Vector3(0, spawnPoint + (-nombreQueens * spacing) + 0.5f, 0);
}
The script when the $$anonymous$$anaging button is clicked :
public void On$$anonymous$$anagingClicked() { background.gameObject.SetActive(false); shop.gameObject.SetActive(false); groupes.gameObject.SetActive(false); leaderboard.gameObject.SetActive(false); managing.gameObject.SetActive(true); //shows the good canvas m$$anonymous$$anager.On$$anonymous$$anaging(PARA$$anonymous$$ETER WHICH ID$$anonymous$$ HOW TO FILL); //triggers another function in the $$anonymous$$anaging$$anonymous$$anager script }
This is the function that's triggered in another script :
public void On$$anonymous$$anaging(int ID)
{
Debug.Log(ID);
Queen tmpQueen = GetComponent<Groupes$$anonymous$$anager>().queens[ID];
tNom.text = tmpQueen.nom;
}
$$anonymous$$y main concern for the moment is not knowing how to fill the parameter from the "On$$anonymous$$anagingClicked function"...
Answer by hectorux · Nov 08, 2018 at 03:13 PM
Try to create a class that store the info, and pass it to the button
I did make a class, and each of my characters are stored in a list, but my main problem is that the $$anonymous$$anaging button is a child of the gameobject that's got the script on it and I don't know how to access it..
You can make a script for every button that gets the character from its parent, somehing like:
public void GetCharacter(){
something.Chracter=transform.parent.getComponent();
} and this script atached to every button and set it on the button listerners list
Thanks for your help !
I've made this script, and each button does get the good informations from its parent.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bouton : $$anonymous$$onoBehaviour {
public string nom;
public string activite;
public string nvXp;
public int popularite;
public int motivation;
public int experience;
public int queenID;
public bool dispo;
public int nbSpectacles;
public int salaire;
// Use this for initialization
void Start ()
{
Queen qScript = gameObject.GetComponentInParent<Queen>();
}
// Update is called once per frame
void Update ()
{
OnVarUpdate();
}
public void OnVarUpdate()
{
nom = gameObject.GetComponentInParent<Queen>().nom;
}
}
I get to launch it within another script, when the button is clicked :
public void On$$anonymous$$anagingClicked()
{
background.gameObject.SetActive(false);
shop.gameObject.SetActive(false);
groupes.gameObject.SetActive(false);
leaderboard.gameObject.SetActive(false);
managing.gameObject.SetActive(true);
m$$anonymous$$anager.On$$anonymous$$anaging(gameObject.GetComponent<Bouton>().queenID);
}
But I'm getting an error :
![alt text][1] [1]: /storage/temp/127454-unity-2018-11-08-23-19-23.png
Which refers to this very line :
m$$anonymous$$anager.On$$anonymous$$anaging(gameObject.GetComponent<Bouton>().queenID);
And I don't really understand what it refers to, because I've always been careful with my objects.
Your answer
![](https://koobas.hobune.stream/wayback/20220612181736im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Colored border of button with GUI skin 0 Answers
RepeatButton AND Button at the same time 1 Answer
Make use of a button in a drop down menu? 0 Answers
How to make unity expect a click 0 Answers