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. Dismiss Notice

Get Screen Y Of A GUI Button / Get Pressed GUI Button - In C# Script Code?

Discussion in 'Scripting' started by FallenAngelSoftware, Jul 6, 2021.

  1. FallenAngelSoftware

    FallenAngelSoftware

    Joined:
    Aug 12, 2018
    Posts:
    58
    Hi,

    Have two questions about the built-in GUI button system:'
    (1) How do we get screen Y of a GUI button(from C# script code)?
    (2) How do we get pressed GUI button value(from C# script code)?

    Let us know, thanks!

    Jesse
     
  2. FallenAngelSoftware

    FallenAngelSoftware

    Joined:
    Aug 12, 2018
    Posts:
    58
    One more question:
    (3) When I run the game, the first button is not selected by default?
     
  3. FallenAngelSoftware

    FallenAngelSoftware

    Joined:
    Aug 12, 2018
    Posts:
    58
    Hi,

    We solved question #3 above:
    - Go into Event System and change "First Selected" to the button you want selected by default...

    Jesse
     
  4. FallenAngelSoftware

    FallenAngelSoftware

    Joined:
    Aug 12, 2018
    Posts:
    58
    Hi,

    We attached the same C# script to every button.
    We are trying to change the Y position of a sprite in the C# script, but it's not working?

    Please view source below:
    Thanks!

    Jesse

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TitleScreenButtonLogic : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.      
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         float buttonScreenY = this.transform.position.y;
    17.  
    18.         Vector3 buttonSelectorLeftPos = GameObject.Find("Button-Selector-Left").transform.position;
    19.  
    20.         buttonSelectorLeftPos.y = buttonScreenY; // Does not set Y position of sprite?
    21.  
    22.     }
    23. }
    24.  
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Of course it won't: Vector3 is a value type, not a reference type. Line 18 above makes a copy.

    If you want you can assign it back into the
    transform.position


    Further reading:

    https://forum.unity.com/threads/hel...a-game-object-with-code.1047332/#post-6779456

    Remember the first rule of GameObject.Find(): DO NOT USE GameObject.Find();

    You have been warned.
     
  6. FallenAngelSoftware

    FallenAngelSoftware

    Joined:
    Aug 12, 2018
    Posts:
    58
    Well, ok...

    What do we use in replacement of GameObject.Find() ?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
  8. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    I recommend trying some of the beginner Unity tutorials:

    https://unity.com/learn

    If you are already an experienced programmer, they can seem pretty basic at times (e.g. teaching you what an if statement is), but they will help you learn how Unity works, and how to do things the Unity Way.

    Changing a components position is one of the earliest things taught in any tutorial.