Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Alter · Jul 22, 2012 at 05:50 PM · c#converting

need help converting JS to C

I just purchased a script from online and i didn't realize it was in JS. Any one willing to translate this for me into C#? I've tried but keep on failing :(

var Point : float; private var GetHitEffect : float; public var targY : float; private var PointPosition : Vector3;

var PointSkin : GUISkin; var PointSkinShadow : GUISkin;

function Start() { Point = Mathf.Round(Random.Range(Point/2,Point*2)); PointPosition = transform.position + Vector3(Random.Range(-1,1),0,Random.Range(-1,1)); targY = Screen.height /2; }

function OnGUI() { var screenPos2 : Vector3 = Camera.main.camera.WorldToScreenPoint (PointPosition); GetHitEffect += Time.deltaTime*30; GUI.color = new Color (1.0f,1.0f,1.0f,1.0f - (GetHitEffect - 50) / 7); GUI.skin = PointSkinShadow; GUI.Label (Rect (screenPos2.x+8 , targY-2, 80, 70), "+" + Point.ToString()); GUI.skin = PointSkin; GUI.Label (Rect (screenPos2.x+10 , targY, 120, 120), "+" + Point.ToString()); }

function Update() { targY -= Time.deltaTime*200; }

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Jul 23, 2012 at 07:28 AM 1
Share

For future reference, Here's some links I found useful in converting between C# and JS :

http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

it is possible to use both languages, but the way they compile means that they only work one way i.e. C# gets compiled before JS, JS can see C# but C# cannot see JS :

http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html => point 3

http://answers.unity3d.com/questions/208383/referencing-a-c-component-script-from-js-script.html

http://answers.unity3d.com/questions/243112/calling-c-classes-from-js.html

avatar image Alter · Jul 23, 2012 at 09:25 AM 0
Share

cheers thanks!

avatar image Muuskii · Jul 23, 2012 at 02:36 PM 0
Share

Excuse me if I'm confused but did you just post code that you have to pay for in a public place?

avatar image Alter · Jul 23, 2012 at 03:39 PM 0
Share

$$anonymous$$y apologies, it was from the asset server for free. Also it's not the complete code just the part that made no complete sense in JS or C#.

avatar image Muuskii · Jul 23, 2012 at 04:09 PM 0
Share

Oh it's okay, I realized it wasn't $1,000,000 code and didn't mean to sound rude if I did. I guess I was just curious. xD

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by ScroodgeM · Jul 22, 2012 at 06:32 PM

using UnityEngine; public class SomeClass : MonoBehaviour { public float Point; private float GetHitEffect; public float targY; private Vector3 PointPosition;

 public GUISkin PointSkin;
 public GUISkin PointSkinShadow;

 void Start()
 {
     Point = Mathf.Round(Random.Range(Point / 2, Point * 2));
     PointPosition = transform.position + new Vector3(Random.Range(-1, 1), 0, Random.Range(-1, 1));
     targY = Screen.height / 2;
 }
 void OnGUI()
 {
     Vector3 screenPos2 = Camera.main.camera.WorldToScreenPoint(PointPosition);
     GetHitEffect += Time.deltaTime * 30;
     GUI.color = new Color(1.0f, 1.0f, 1.0f, 1.0f - (GetHitEffect - 50) / 7);
     GUI.skin = PointSkinShadow;
     GUI.Label(new Rect(screenPos2.x + 8, targY - 2, 80, 70), "+" + Point.ToString());
     GUI.skin = PointSkin;
     GUI.Label(new Rect(screenPos2.x + 10, targY, 120, 120), "+" + Point.ToString());
 }
 void Update()
 {
     targY -= Time.deltaTime * 200;
 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alter · Jul 23, 2012 at 06:12 AM 0
Share

thanks a lot guys! I'll give this an shot and let you know the outcome. :)

avatar image
0

Answer by aldonaletto · Jul 22, 2012 at 06:54 PM

Basically, in C# you needed to place a new keyword before struct creators like Vector3(...) and Rect(...):

public float Point; private float GetHitEffect; public float targY; private Vector3 PointPosition;

public GUISkin PointSkin; public GUISkin PointSkinShadow ;

void Start() { Point = Mathf.Round(Random.Range(Point/2,Point*2)); // C# requires the "new" keyword before any structure PointPosition = transform.position + new Vector3(Random.Range(-1,1),0,Random.Range(-1,1)); targY = Screen.height /2; }

void OnGUI() { // Camera.main IS a camera reference! No need for .camera Vector3 screenPos2 = Camera.main.WorldToScreenPoint (PointPosition); // this instruction makes no sense in OnGUI! // maybe it should be moved to Update GetHitEffect += Time.deltaTime*30; GUI.color = new Color (1.0f,1.0f,1.0f,1.0f - (GetHitEffect - 50) / 7); GUI.skin = PointSkinShadow; // Rect also needs a "new" keyword in C# GUI.Label (new Rect (screenPos2.x+8 , targY-2, 80, 70), "+" + Point.ToString()); GUI.skin = PointSkin; GUI.Label (new Rect (screenPos2.x+10 , targY, 120, 120), "+" + Point.ToString()); }

void Update() { targY -= Time.deltaTime*200; } NOTE: As said in the comments, there's a weird instruction in OnGUI that adds Time.deltaTime*30 to GetHitEffect - this makes no sense, and will increase GetHitEffect in an unpredictable speed because OnGUI is called 2 to 4 times between frames. Maybe moving this instruction in Update could at least make it work predictably.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Converting script to C# 1 Answer

problem with converting js to c# 2 Answers

Converting JavaScript to C# 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges