hey,
I just can’t seem to wrap my mind around how you would make a button click change a toggle to intractable or not intractable.
The other thing i can’t get my head around is how you make a button click make a toggle switch visible or not visible.
I have done this type of thing with other UI elements but for some reason i keep running into issues attempting to make it work with the toggle switches.
Could someone give me an idea where I would get started making a script for a button that would make a toggle switch visible or not visible and/or intractable and not intractable?
Check out the script references here to get the methods for each component:
Overview: Redirecting to latest version of com.unity.ugui
Toggle: http://docs.unity3d.com/Manual/script-Toggle.html
Button: Redirecting to latest version of com.unity.ugui
You’ll want a method that runs when the Button is clicked which accesses the interactable property of the Toggle and assigns it appropriately.
To change the visibility of the toggle switch you can toggle the SetActive function of the gameobject that the toggle component is attached too to True or False depending on what you want.
Hope that helps!
1 Like
thank you for the links and the short explanation i think that i can work it out now. i was messing up with the logic and how they interacted i think. appreciate it!
So this is what i worked out to do code wise.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class buyPoleA : MonoBehaviour {
public Toggle poleToggleA;
public UnityEngine.UI.Text poleInfo;
void Start()
{
poleInfo.text = ("Coins: " + GameControl.control.fishingPoleACost);
}
void Update()
{
}
void OnClick()
{
if(GameControl.control.coins >= GameControl.control.fishingPoleACost)
{
GameControl.control.coins -= GameControl.control.fishingPoleACost;
poleToggleA.interactable = true;
// needs to access the interactable property of the toggle and assignes it appropriatly.
}
}
}
It works great and as expected with 1 exception. I have a button in my scene I also have 2 or more toggle switches. All the toggle switches start as not interactable except for the 1st one. What I would like to do is once you push the button and make the 2nd toggle interactable it stays interactable even if you tick the top one for a little bit. Right now when you tick the top one and the other one un ticks (it is a member of a toggle group) Then goes right back to not interactable.
what would you suggest ?