- Home /
How can I reference a Toggle?
Hello community.
How can I reference a toggle with the new canvas UI? I've tried to reference by name and tag but it doesn't Works.
Here's a piece of my code.
public void OpcionesKeyboard()
{
accelerometerJugador1.gameObject.name.Equals("accelerometerJugador1");
keyboardJugador1.gameObject.name.Equals("keyboardJugador1");
if (keyboardJugador1.onValueChanged.Equals(true))
{}
else if (keyboardJugador1.onValueChanged.Equals(false))
{
accelerometerJugador1.onValueChanged.Equals(true);
}
or
public void OpcionesKeyboard()
{
accelerometerJugador1.gameObject.tag.Equals("accelerometerJugador1UI");
keyboardJugador1.gameObject.tag.Equals("keyboardJugador1UI");
if (keyboardJugador1.onValueChanged.Equals(true))
{}
else if (keyboardJugador1.onValueChanged.Equals(false))
{
accelerometerJugador1.onValueChanged.Equals(true);
}
Thanks for the help.
Regards.
Answer by rhynodegreat · May 25, 2015 at 04:24 PM
You need to use using UnityEngine.UI
at the top of the file. Then you can just create a public Toggle myToggle
field that can be assigned in the inspector.
I've created it with "private Toggle keyboardJugador1" but I don't know where to assign it on the inspector.
The place "On value Changed (Boolean)" is for the function. But there's a place for reference it?
I mean I've tried to assign by tag and name but it's useless. There's another way? If there are, can you show me where it is?
Thanks rhynodegreat.
If you make it public or use the [System.SerializeField]
attribute, then you can assign it in the inspector. Also, in the inspector for the toggle, you can set it up so that it calls the function you want.
Good to know it. Thanks, I'll try and see how it Works. Edit: By the way, what reference use "[System.SerializeField]" attribute?
Answer by Phenoxon · May 25, 2015 at 06:00 PM
You will probably want to use GameObject.Find() to locate your Toggle by name:
var myToggle : GameObject;
// This will return the game object named Hand in the scene.
hand = GameObject.Find("myToggle");
Other similar functions (such as FindGameObjectsWithTag, FindObjectOfType, FindObjectsOfType, etc.) are listed in the manual entry for GameObject.
To find all active Toggles by tag:
Toggle[] myToggles = GameObject.FindGameObjectsWithTag ("myToggleTag");
To find all Toggles in the scene (irrespective of tag), you can use:
Toggle[] myToggles = FindObjectsOfType(typeof(Toggle)) as Toggle[];
Hope this helps.
P.S. To expose a variable in the Inspector, you will need to make it Public.
P.S.2 I'm new to Unity as well. Treat my answers with some suspicion as they might not be correct/efficient.
That's a good point to know. But I'm declaring the class Toggle like Private Toggle "name" to call its methods. Then if I declare it like GameObject I can't do nothing with Toggle class.
The problem is that by code I can't search that Toggle on my hierarchy. I'll like to have inspector more clean and don't do assigns of grab and drop.
By the moment I've assigned with Public declaration on inspector with drag and drop on my script like you told me.
Thanks for the help.
If anyone know how to assign by code it will be awesome.
Answer by BlueAero · Jun 13, 2016 at 04:11 AM
GameObject toggle; Toggle soundToggle;
void Start() { toggle = GameObject.FindGameObjectWithTag("Sound"); soundToggle = toggle.GetComponent();
SetToggles(); }
void SetToggles() { if (soundOn) { soundToggle.isOn = true; } else { soundToggle.isOn = false; } } // end SetToggles()
Your answer
Follow this Question
Related Questions
How Can I save my values with Toggles in UI canvas 4.6 through scenes? 3 Answers
UI canvas, toggle question 0 Answers
UI button does not press if colliding with other UI object 1 Answer
Canvas dropdown is buggy and i don't know how to solve it ;-; 0 Answers
Using raycast to enable canvas that is linked to a prefabs gameobject 0 Answers