Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Change input

Discussion in 'Scripting' started by psyc0rt, Jun 23, 2015.

  1. psyc0rt

    psyc0rt

    Joined:
    Aug 9, 2013
    Posts:
    5
    Code (JavaScript):
    1.  
    2.         void Update()
    3.         {
    4.                 if (Input.GetKeyDown(KeyCode.S))
    5.                 {
    6.                         m_TankOneActive = !m_TankOneActive;
    7.                         SetTanksActive();
    8.                 }
    9.         }
    10.  
    11.  
    This is a part of my code its working fine, but I wanna change the input to a GUI Button in the canvas.
    I tried around with GetButtonDown("NameOfMyButton") but it does not work. My programming skills are bit low, so I hope somebody could help me.
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    You dont work with GUI buttons like that. If you look at the GUI button you will see there is an OnClick property. You need to assign the method you want to be called there.

    UI Button information
     
  3. psyc0rt

    psyc0rt

    Joined:
    Aug 9, 2013
    Posts:
    5
    Unfortunately is there no possibility to choose any function like on my other Buttons. I guess the reason is that there is no Function in my script. Does anybody know what I should add or change in my C# script ?

    Code (CSharp):
    1. using UnityEngine;
    2. public class SwitchWithMouse : MonoBehaviour
    3. {
    4.     private GameObject m_Tanks;
    5.     private Transform m_TankOne;
    6.     private Transform m_TankTwo;
    7.     private bool m_TankOneActive = true;
    8.     void Start()
    9.     {
    10.         m_Tanks = GameObject.Find("Tanks");
    11.         foreach (Transform t in m_Tanks.transform)
    12.         {
    13.             if (t.name == "TankOne")
    14.                 m_TankOne = t;
    15.             if (t.name == "TankTwo")
    16.                 m_TankTwo = t;
    17.         }
    18.         SetTanksActive();
    19.     }
    20.     void Update()
    21.     {
    22.         //if (Input.GetKeyDown(KeyCode.S))
    23.         if (Input.GetButtonDown("Fire1"))
    24.         {
    25.             m_TankOneActive = !m_TankOneActive;
    26.             SetTanksActive();
    27.         }
    28.     }
    29.     private void SetTanksActive()
    30.     {
    31.         m_TankOne.gameObject.SetActive(m_TankOneActive);
    32.         m_TankTwo.gameObject.SetActive(!m_TankOneActive);
    33.     }
    34. }
     
  4. kmgr

    kmgr

    Joined:
    Mar 5, 2015
    Posts:
    9
    Make the SetTanksActive() method public and it should show up in the event inspector.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    put lines 25 and 26 into a public function, call the function from where line 25 is now... call the same function in the OnClick event in the button's inspector.

    Making any code into a stand alone function is really just a copy/paste :p
     
  6. psyc0rt

    psyc0rt

    Joined:
    Aug 9, 2013
    Posts:
    5
    Thank you I did it, see you in the stars