Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UGUI button/script woes

Discussion in 'Scripting' started by Roughrider, May 2, 2016.

  1. Roughrider

    Roughrider

    Joined:
    Jul 17, 2011
    Posts:
    73
    I'm having a bit of trouble with this one, considering I got it to work ONE time using the exact C# script I've written here.

    I'm really lost here, and I'm revisiting beginning C# after a long hiatus.
    I have two buttons in my scene on a canvas, in a panel. I am using an empty object in my scene named "ButtonMaster" with a script named "ButtonScript" attached to it.
    I have a separate script in my scene named "Bools" attached to an empty gameobject named and tagged as "GameMaster".
    I'm using the two buttons to toggle the boolean values on the GameMaster object.
    My On Click() in the button inspector has the "ButtonMaster" object assigned, and then the ButtonScript.ClickedBuryUnbury assigned on button one and ButtonScript.ClickedKillRezz on the second button respectably.
    I've gotten these buttons to properly toggle the boolean values within the GameMaster object on and off ONE time. (super weird)
    Anyhow, here is the script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ButtonScript : MonoBehaviour {
    5.  
    6.     private GameObject gameMaster;
    7.     private bool killRezzToggle;
    8.     private bool buryUnburyToggle;
    9.    
    10.     void Start () {
    11.         gameMaster = GameObject.FindGameObjectWithTag ("GameMaster");
    12.         killRezzToggle = gameMaster.GetComponent<Bools>().amIDead;
    13.         buryUnburyToggle = gameMaster.GetComponent<Bools>().buriedInGround;
    14.     }
    15.        
    16.     public void ClickedKillRezz()
    17.     {
    18.         killRezzToggle=!killRezzToggle;
    19.     }
    20.  
    21.     public void ClickedBuryUnbury()
    22.     {
    23.         buryUnburyToggle=!buryUnburyToggle;
    24.     }
    25. }
    Can anyone tell me whats going on? Perhaps I'm just coding the toggle incorrectly, but why would it have worked on one occasion? Thanks everyone!
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    You're just changing the private bools in the ButtonScript.

    It won't change the Bools script unless you assign them back like:

    Code (csharp):
    1. killRezzToggle = !killRezzToggle;
    2. gameMaster.GetComponent<Bools>().amIDead = killRezzToggle;
    I would just get the Bools component in Start and just set it directly.

    Code (csharp):
    1. private Bools _bools;
    2.  
    3. void Start ()
    4. {
    5.     _bools = GameObject.FindGameObjectWithTag("GameMaster").GetComponent<Bools>();
    6. }
    7.  
    8. void ToggleKill()
    9. {
    10.     _bools.amIDead = !_bools.amIDead;
    11. }
    Or just move the functions into the Bools script, and point the button OnClick to there.
     
    Last edited: May 2, 2016
    Roughrider likes this.
  3. Roughrider

    Roughrider

    Joined:
    Jul 17, 2011
    Posts:
    73
    Stardog, you are awesome. The reason I'm not doing it in the bools script itself (I know how to get it done there) is that I'm trying and typing all different types of code, and specifically trying to get the hang of the accessing other gameobjects variables and manipulate them. Very much a beginner. I'm still wondering why it fluked and that script ran once without a hitch... Weird.