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

Converting Java to C#

Discussion in 'Scripting' started by cristo, Nov 19, 2014.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi, I'm new to coding and am trying to turn a Java script into a C#. I know it must be simple to someone who knows their stuff, but I've spent hours going down false paths. If anyone can please give me a hint, that would be great. (I'm attempting to trigger GUI elements by the Player)


    var note : GameObject;

    function Start (){

    note.SetActive(false);
    }

    function On TriggerEnter(){
    note.SetActive(true);
    }

    function OnTriggerExit (){

    note.SetActive(false);
    }

    My latest try at translating this as C# was this:

    Transform Text;

    void Start () {

    Transform Text = false;

    }

    void OnTriggerEnter (Collider other) {

    Transform Text = true;

    }

    void OnTriggerExit (Collider other)
    {

    Transform Text = false;
    }
    }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Code (JavaScript):
    1. var note : GameObject;
    2.  
    3. function Start (){
    4.  
    5.   note.SetActive(false);
    6. }
    7.  
    8. function On TriggerEnter(){
    9.   note.SetActive(true);
    10. }
    11.  
    12. function OnTriggerExit (){
    13.  
    14.   note.SetActive(false);
    15. }
    Code (CSharp):
    1.  
    2. public GameObject note;
    3.  
    4. void Start (){
    5.  
    6.   note.SetActive(false);
    7. }
    8.  
    9. void OnTriggerEnter(Collider other){
    10.   note.SetActive(true);
    11. }
    12.  
    13. void OnTriggerExit (Collider other){
    14.  
    15.   note.SetActive(false);
    16. }
     
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265

    Hey hpjohn,

    Thanks for taking the time to help me out, and giving me some insight. Thanks for your help.

    I'm not sure what the 'note' bit is, some kind of variable particular to GUI.

    Chris.