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

How do I link make visible a UI Button THAT ALREADY EXISTS through a C# code?

Discussion in 'Getting Started' started by MattCarter24, Jun 16, 2015.

  1. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Hello,

    I am a beginner so please make sure you explain everything easily.

    I would like to know what code is necessary to make a pre-existing UI button visible at the end of a pre-existing C# Script. PS: Does this mean the button has to be set to invisible first? How do I do any of this. Is any of this even possible?

    Please help! - Any help would be much appreciated! :)
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It sounds as if you ask us to help you with your homework...
     
  3. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    @Dantus it is part of my secondary school education. Yes. Are you an expert at coding who can help me? @Dantus
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Almost everyone who has implemented something in Unity is able to help you with that, because it is relatively simple. It sounds as if you haven't studied the course material for that task yet and if you did, you should share a little more information and tell us what exactly is not clear.
    Just to make it clear, I won't do your work, but I can help if you have some more precise questions.
     
  5. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    All the stuff you are asking you can learn with 3 hours of learning the basic form the unity learning site and reading the script documentation for the corresponding components..
     
  6. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,386
  9. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    You seem to be under the influence that I can learn this code from a course. I'm sorry to inform you but that's incorrect. I am left to fend for myself and believe me I have watch tutorials but I am so used to JavaScript it is hard to adapt. Thats why I want help.
     
  10. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,386
    Why do you think you can't learn this?
     
  11. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    I can learn online, I'm just saying I am expected to learn everything I ask on this forum on my own and I'm struggling to understand everything. I have a deadline to meet as well.
     
  12. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,386
    Because you need to learn how to find information on your own. You cannot expect to be handed everything, especially things you could easily find on your own. If you are uninterested in doing tutorials to better yourself then you can always go directly to the API reference to find the code you need.

    http://docs.unity3d.com/ScriptReference/
     
  13. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    In my opinion, you are trying to achieve something that is too complicated for you. You should not ask others to code for you, but you should create it on your own. That is the only way to learn coding. Make experiments, try things out, that's how you learn it.
     
    Aurore likes this.
  14. Aurore

    Aurore

    Director of Real-Time Learning Unity Technologies

    Joined:
    Aug 1, 2012
    Posts:
    3,106
    Please listen to our wise community members and try learning things for yourself and ask questions along the way, everyone here help others in their spare time and are not here to be personal tutors.
     
  15. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Agreed on all counts, but still, let me try to give him more of a nudge in the right direction.

    There is more than one way to make a button visible/invisible. The easiest is to set it active (to make it visible) or inactive (to make it invisible). You can see how this is done in the example code for SetActive.

    Note that it can also be done without any code at all, using UnityEvents... see the tutorial in my sig.

    If you try this and it doesn't work, then please post the code you have so far, along with the error message (or misbehavior) you got, and what you think you understand about it.

    Good luck,
    - Joe
     
    Dantus and Aurore like this.
  16. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
  17. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Hello,

    I have the code below which works perfectly. I would like to somehow (in a new scene) pull the score variable from the previous code and display it in a UI text. I would also like the new script in the new scene to run automatically without any triggers?

    Any help would be much appreciated!

    Here is my code...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class CheckAnsL8 : MonoBehaviour
    5. {
    6.    
    7.     public InputField iField;
    8.     string myName;
    9.     string myText;
    10.     public int score = 0;
    11.    
    12.     public Text Status;
    13.    
    14.     GameObject myTextgameObject; // gameObject in Hierarchy
    15.     Text ourComponent;           // Our refference to text component
    16.    
    17.     void Start()
    18.     {
    19.         if (PlayerPrefs.HasKey("Score") == true)
    20.         {
    21.             score = PlayerPrefs.GetInt("Score");
    22.         }
    23.        
    24.         // Find gameObject with name "MyText"
    25.         myTextgameObject = GameObject.Find("MyText");
    26.         // Get component Text from that gameObject
    27.         ourComponent = myTextgameObject.GetComponent<Text>();
    28.     }
    29.    
    30.     public void MyFunction()
    31.     {
    32.         Debug.Log(iField.text);
    33.         myName = iField.text;
    34.         if (myName == "castle")
    35.         {
    36.             score++;
    37.             Debug.Log("Correct! The word 'castle' is correct!");
    38.             Debug.Log("Your score is now:");
    39.             Debug.Log(score);
    40.             PlayerPrefs.SetInt("Score", score);
    41.             Status.text = "Correct!";
    42.             Status.color = Color.green;
    43.         }
    44.         else
    45.         {
    46.             score--;
    47.             Debug.Log("Incorrect! The answer was 'castle'.");
    48.             Debug.Log("Your score is now:");
    49.             Debug.Log(score);
    50.             PlayerPrefs.SetInt("Score", score);
    51.             Status.text = "Incorrect!";
    52.             Status.color = Color.red;
    53.         }
    54.        
    55.         GameObject theTargetChild = GameObject.Find("theTargetChild");
    56.         foreach (Transform child in theTargetChild.transform)
    57.         {
    58.             child.gameObject.SetActive(true);
    59.         }
    60.     }
    61.    
    62.     void Update () {
    63.         ourComponent.text = score.ToString();
    64.     }
    65. }
     
  18. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    FIXED IT! YAY