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

Java Liftscript into C#

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

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi, I thought I might be able to work it out, but I'm getting really tangled trying to make this work!

    There's a Java script for a lift and I'm trying to convert it to C#. I think there's a error in the C# on the line

    gameObject.GetComponent<Animation>("UP");

    But I'm sure I'm doing other things very wrong.

    I'll put the Java script and then my C# effort. If anyone could please point out the error, I'd be so appreciative.

    Hopefully I can learn enough to help myself next time....

    JAVA:

    private var pressedButton : boolean = false;
    private var isElevatorUp : boolean = false;

    var target : GameObject;

    function OnMouseOver ()
    {
    pressedButton = true;
    }
    function OnMouseExit ()
    {
    pressedButton = false;
    }
    function OnMouseDown ()
    {
    if (isElevatorUp == false)
    {
    target = GameObject.Find ("Elevator");
    target.animation.Play ("UP");
    isElevatorUp = false;
    }
    }
    function OnGUI ()
    {
    if (pressedButton == true)
    {
    GUI.Box (new Rect(300, 300, 200, 20), "Press to use lift!");
    }
    }

    My C# attempt...

    private bool pressedButton = false;
    private bool isElevatorUp = false;
    public GameObject Elevator;

    void OnMouseOver ()

    {
    pressedButton = true;
    }
    void OnMouseExit ()
    {
    pressedButton = false;
    }
    void OnMouseDown ()
    {
    if (isElevatorUp == false)
    {
    Gameobject Elevator;
    gameObject.GetComponent<Animation>("UP");
    isElevatorUp = true;
    }
    else
    {
    Gameobject Elevator;
    gameObject.GetComponent<Animation>("Down");
    isElevatorUp = false;
    }
    }
    void OnGUI ()
    {
    if (pressedButton == true)
    {
    GUI.Box (new Rect(300, 300, 200, 20), "Press to use lift!");
    }
    }
     
  2. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public name me : MonoBehaviour {
    5. private bool  pressedButton = false;
    6. private bool  isElevatorUp = false;
    7.  
    8. GameObject target;
    9.  
    10. void  OnMouseOver (){
    11. pressedButton = true;
    12. }
    13. void  OnMouseExit (){
    14. pressedButton = false;
    15. }
    16. void  OnMouseDown (){
    17. if (isElevatorUp == false)
    18. {
    19. target = GameObject.Find ("Elevator");
    20. target.animation.Play ("UP");
    21. isElevatorUp = false;
    22. }
    23. }
    24. void  OnGUI (){
    25. if (pressedButton == true)
    26. {
    27. GUI.Box (new Rect(300, 300, 200, 20), "Press to use lift!");
    28. }
    29. }

    This maybe?
    Really not much of a way for me to test it though.

    Don't forget to give it a name where I put in "name me".
     
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Nubz, you save me again. I feel a bit bad about it! I'll give it a test. Post results.
     
  4. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    No problem.
    Only thing is I'm not always right when I convert them over so there might be a mistake or 2 in there.
     
  5. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    No worries, I need to do some of the work! Thanks for the insights. It works fine.

    I managed to add the

    else{
    target = GameObject.Find ("Elevator");
    target
    .animation.Play ("Down");
    isElevatorUp = true;
    }

    Hooray for me! Thanks again.