Search Unity

Java ( Unity Script ) to C# - Im fairly new to this

Discussion in 'Getting Started' started by Deleted User, Jun 22, 2019.

  1. Deleted User

    Deleted User

    Guest

    Hello , Im following a tutorial that was made in " java script " and I was kinda translating it well up until now. I was hoping someone can translate or help me understand this script so I can apply it to C#. It's supposed to help make my scenes or sections randomly generate. Or if you have an easier/ updated script, that would be great too.

    var Section01 : GameObject;
    var Section02 : GameObject;
    var Section03 : GameObject;

    var NewXAxis : float = NextAxis.TheXAxis;
    var GenSec : int;
    var NewSec : GameObject;
    var NextPosition : Vector3 = Vector3(NextAxis.TheXAxis, 25.9, 16.7916);

    function OnTriggerEnter (col : Collider) {
    NextPosition = Vector3(NextAxis.TheXAxis, 25.9, 16.7916);
    GenSec = Random.Range(1, 4);
    NewXAxis = NextAxis.TheXAxis;

    if (GenSec == 1) {
    NewSec = Section01;
    }
    if (GenSec == 2) {
    NewSec = Section02;
    }
    if (GenSec == 3) {
    NewSec = Section03;
    }

    Instantiate(NewSec, NextPosition, Quaternion.identity);
    NextAxis.TheXAxis += 500;
    }
    ----------------------------------------------------------------------------
    //It is also working with this script, but I've already translated this one

    static var TheXAxis : float = 216.3578;
    var InternalAxis : float;

    function Update () {
    InternalAxis = TheXAxis;
    }


    // Into this one


    {
    public double TheXAxis = 163.593;
    public double InternalAxis;

    void Update()
    {
    InternalAxis = TheXAxis;

    }
    }
     
  2. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Try out some websites that convert javascript to C#. Remember to save your original code in case something goes wrong and test the script afterwards.

    A website I found that might help:
    https://www.m2h.nl/files/js_to_c.php
     
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    I tried, multiple sites, that one included lol. The only thing that it doesnt like is the 'NextAxis.TheAxis' - Which comes from the script below... It says " An object refference is required for the non-static field,method or property ' NextAxis.TheAxis'.
    The console error is :CS0120

    using UnityEngine;

    public class NextAxis : MonoBehaviour
    {
    public double TheXAxis = 163.593;
    public double InternalAxis;

    void Update()
    {
    InternalAxis = TheXAxis;

    }
    }
     

    Attached Files:

  4. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Looking at your code, that line just seems wrong (I dont know Javascript, but it looks wrong in C#). I don't think there is actually a way you can translate that, but instead you will have to code it in yourself. The issue is that TheXAxis isnt static, so there isnt a number and you need to find one in the scene. However, as the error says, you do not search for an object in the scene. You need to do something like GameObject(Object with the script).GetComponent<NextAxis>.TheXAxis.
     
    Deleted User likes this.
  5. Deleted User

    Deleted User

    Guest

    It seems like what you're saying makes sense but the problem is I'm too much of a beginner to know how to fix it, I really only know the basics haha, but thanks for your help.Ill keep trying with that line you gave me.
     
  6. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Okay so let me try to explain a bit more in depth. If you have another class that isn't static, you cant access its variables unless the variables are static. To fix this, you will need an object reference. Just like how you do public double, you will need a public with that class as the second word. Then you can access that class based on the object it is attached to in the scene. However, if you don't plan on making an object in your scene with that class, you might want to look into making your variables static.
     
    Deleted User likes this.
  7. Deleted User

    Deleted User

    Guest

    So just add 'static' before all my variables ?
     
  8. Deleted User

    Deleted User

    Guest

    So I tried adding 'TheXAxis' as a static double variable, and removed ' NextAxis' from 'NextAxis.TheXAxis' which seemed to get rid of the red underlines but Im guessing without NextAxis, it wont reference the other script right ?
     

    Attached Files:

  9. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    well you still need NextAxis to access its static variables
     
    Deleted User likes this.
  10. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    You just dont need to do any getcomponent or object referencing
     
    Deleted User likes this.
  11. Deleted User

    Deleted User

    Guest

    I dont need Getcomponent ? but I thought you just said "You need to do something like GameObject(Object with the script).GetComponent<NextAxis>.TheXAxis." ahaha
     
  12. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    That is if you dont use a static. You can either use a static, or use a getcomponent and reference an object.
     
    Deleted User likes this.
  13. Deleted User

    Deleted User

    Guest

    Ok, So I tried making NextAxis.TheXAxis static but the error still now reads " ' NextAxis.TheXAxis' is inaccessible due to its protection level".
     
  14. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    is it public?
     
    Deleted User likes this.
  15. Deleted User

    Deleted User

    Guest

    It is static double
     
  16. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    You need public static double
     
    Deleted User likes this.
  17. Deleted User

    Deleted User

    Guest

    Wow, it actually worked , the red underlines are gone .... idk if im happy we solved that or mad it was that easy lol. Thank you very very much
     
    Last edited by a moderator: Jun 23, 2019
  18. Deleted User

    Deleted User

    Guest

    Umm ... the Vector3 had a problem the whole time also ( Non-Invocable member 'Vector 3' cannot be used like a method ) but like you said earlier the script looks a bit wonky, im just happy to have solved the axis problem... buuuut, if you had any suggestions on that too, itd be appreciated ahaha. You dont have to though, trust me, you've helped enough, made my migraine go away.
     

    Attached Files:

  19. Deleted User

    Deleted User

    Guest

    Nevermind ! I just added ' new' to vector3 " new Vector3 and it appears to have worked. Thanks again for everything, your patience included. :)

    And on top of that, I tested it , and it actually works, it generates the new scenes for me. If I end up completing the game, I will surely credit you or ' Bunny Productions ' haha