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 incorporate the code I found for something into my existing code?

Discussion in 'Editor & General Support' started by MattCarter24, Jun 13, 2015.

  1. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Hello,

    I am a beginner so please make your explanations simple. I am also coding in C#.

    I would like to get a score variable to output into a pre-existing UI Text. I found some useful code at http://answers.unity3d.com/questions/861478/inputting-a-variable-into-ui-text.html but I don't know how do incorporate into my current code? I tried but had many compiler errors and ended up removing it to consult the forum.

    The code I currently have is...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class CheckAnsL1 : MonoBehaviour
    5. {
    6.  
    7.     public InputField iField;
    8.     string myName;
    9.     string myText;
    10.     public int score = 0;
    11.    
    12.     void Start()
    13.     {
    14.         if (PlayerPrefs.HasKey("Score") == true)
    15.         {
    16.             score = PlayerPrefs.GetInt("Score");
    17.         }
    18.     }
    19.    
    20.     public void MyFunction()
    21.     {
    22.         Debug.Log(iField.text);
    23.         myName = iField.text;
    24.         if (myName == "city")
    25.         {
    26.             score++;
    27.                 Debug.Log("Correct! The word 'city' is correct!");
    28.             Debug.Log("Your score is now:");
    29.             Debug.Log(score);
    30.             PlayerPrefs.SetInt("Score", score);
    31.         }
    32.         else
    33.         {
    34.             score--;
    35.                 Debug.Log("Incorrect! The answer was 'city'.");
    36.             Debug.Log("Your score is now:");
    37.             Debug.Log(score);
    38.             PlayerPrefs.SetInt("Score", score);
    39.         }
    40.     }
    41. }
    It works perfectly.

    Now I want to put this code into it. REMEMBER" Please tell me exactly what line to place it on or if it neds to go into a separate script of its own.

    Code (CSharp):
    1.     GameObject myTextgameObject; // gameObject in Hierarchy
    2.      Text ourComponent;           // Our refference to text component
    3.      
    4.      void Start () {
    5.          // Find gameObject with name "MyText"
    6.          myTextgameObject = GameObject.Find("MyText");
    7.          // Get component Text from that gameObject
    8.          ourComponent = myTextgameObject.GetComponent<Text>();
    9.          // Assign new string to "Text" field in that component
    10.          ourComponent.text = "Hey, I'm some randoms score !!";
    11.      }
    I want the score variable in the current script to constantly update and show in a UI text from the beginning of the scene, and for this to work across multiple scenes.

    Please help! - You're help would be much appreciated! :)
     
  2. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    Since this is one of the most basic things to do in c# and unity, maybe you should try your own again and post the compiler error. That way somebody can tell you what you did wrong and you can learn a bit.
    If you want to build your own car, you have to know why scratching a screw with a spoon won't work.
     
    Dantus likes this.
  3. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    But I don't even know where to paste the code in the first place? Please just tell me. If there are any further compiler errors I will upload a screenshot. @Carpe Denius .
     
  4. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    @Lee7 Please help mehere too :)
     
  5. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    As soon as you understand what each line of code does, you know where to paste it.
    Basically, change every Debug.Log(score); with ourComponent.text=score.ToString();
    You will get an error that ourComponent is not available, so you have to search for a place where you can init the text component. I'll leave that to you, some homework ;)
     
  6. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    But @Carpe Denius I can't just change every Debug.Log(score); with ourComponent.text=score.ToString(); because I can't even get the rest of the code which is
    Code (CSharp):
    1. GameObject myTextgameObject; // gameObject in Hierarchy
    2. Text ourComponent; // Our refference to text component
    3. void Start () {
    4. // Find gameObject with name "MyText"
    5. myTextgameObject = GameObject.Find("MyText");
    6. // Get component Text from that gameObject
    7. ourComponent = myTextgameObject.GetComponent<Text>();
    8. // Assign new string to "Text" field in that component
    9. ourComponent.text = "Hey, I'm some randoms score !!";
    10. }
    Because the script it is in now already has a void Start() so do i just add all of the new code above under the existing code's Start()? I AM SO CONFUSED??? Please help :)
     
  7. Lee7

    Lee7

    Joined:
    Feb 11, 2014
    Posts:
    136
    You need to take at least some basic tutorials in programming, C# to be specific. We cant do everything for you, we have our own stuff we are working on.

    We provided you with the information you need to do what you want, as to exactly how to do it, you will need to learn yourself.
     
  8. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    @Lee7 I have learnt now. This code below

    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class CheckAnsL1 : MonoBehaviour
    5. {
    6. public InputField iField;
    7. string myName;
    8. string myText;
    9. public int score = 0;

    10. GameObject myTextgameObject; // gameObject in Hierarchy
    11. Text ourComponent; // Our refference to text component
    12. void Start()
    13. {
    14. if (PlayerPrefs.HasKey("Score") == true)
    15. {
    16. score = PlayerPrefs.GetInt("Score");
    17. }

    18. // Find gameObject with name "MyText"
    19. myTextgameObject = GameObject.Find("MyText");
    20. // Get component Text from that gameObject
    21. ourComponent = myTextgameObject.GetComponent<Text>();
    22. }
    23. public void MyFunction()
    24. {
    25. Debug.Log(iField.text);
    26. myName = iField.text;
    27. if (myName == "city")
    28. {
    29. score++;
    30. Debug.Log("Correct! The word 'city' is correct!");
    31. Debug.Log("Your score is now:");
    32. Debug.Log(score);
    33. PlayerPrefs.SetInt("Score", score);
    34. }
    35. else
    36. {
    37. score--;
    38. Debug.Log("Incorrect! The answer was 'city'.");
    39. Debug.Log("Your score is now:");
    40. Debug.Log(score);
    41. PlayerPrefs.SetInt("Score", score);
    42. }
    43. }

    44. void Update () {
    45. ourComponent.text = score.ToString();
    46. }
    47. }
    Only issue is that it creates an infinite number of the error below in the console log.
    upload_2015-6-16_19-9-37.png
     
  9. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    Do you get some other error messages, maybe that myText doesn't exist or is this the only one?
     
  10. MattCarter24

    MattCarter24

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

    MattCarter24

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

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    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?
     
  13. Lee7

    Lee7

    Joined:
    Feb 11, 2014
    Posts:
    136
    IIRC, if you want to make a GUI object invisible you can just disable it via script and enable it when you want.
     
  14. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    @Lee7 what is IIRC? I assume that is the line of code I need to make a GUI object invisible you can just disable it via script and enable it when you want.
     
  15. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    @Lee7 & @CarpeDenius thanks for your help! I fixed all the errors myself! So proud! :) I thank you for your help so far and will contact you if needed in the future! Thanks again!
     
  16. 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. }
     
  17. MattCarter24

    MattCarter24

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