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
0
Question by mvp_mike13 · Dec 11, 2013 at 02:36 AM · javascriptc# to javascript

My Health Script Won't work in C#

I'm trying to convert my Javascript to C# but it won't work.

Javascript:

 var backgroundTexture : Texture; //input image lftTop4
 var foregroundTexture : Texture; //input image lftTop2
 var foregroundTexture2 : Texture; //input image lftTop3
 var frameTexture : Texture; //input image lftTop1
  
 var healthWidth: int = 52;
 var healthHeight: int = 234;
  
 var healthMarginLeft: int = 41;
 var healthMarginTop: int = -76;
  
  var manaWidth: int = 81;
 var manaHeight: int = 160;
  
 var manaMarginLeft: int = 25;
 var manaMarginTop: int = -39;
  
 var frameWidth : int = 195;
 var frameHeight: int = 195;
  
 var frameMarginLeft : int = -14;
 var frameMarginTop: int = -39;
  
 function OnGUI () {
  
     GUI.DrawTexture( Rect(frameMarginLeft,frameMarginTop, frameMarginLeft + frameWidth, frameMarginTop + frameHeight), backgroundTexture, ScaleMode.ScaleToFit, true, 0 );
  
     GUI.DrawTexture( Rect(healthMarginLeft,healthMarginTop,healthWidth + healthMarginLeft, healthHeight), foregroundTexture, ScaleMode.ScaleAndCrop, true, 0 );
  
  GUI.DrawTexture( Rect(manaMarginLeft,manaMarginTop,manaWidth + manaMarginLeft, manaHeight), foregroundTexture2, ScaleMode.ScaleAndCrop, true, 0 );
  
     GUI.DrawTexture( Rect(frameMarginLeft,frameMarginTop, frameMarginLeft + frameWidth,frameMarginTop + frameHeight), frameTexture, ScaleMode.ScaleToFit, true, 0 );
  
 }


Attempt at C# convert:

 using UnityEngine;
 using System.Collections;
 
 public class MYCLASSNAME : MonoBehaviour {
 Texture backgroundTexture; //input image lftTop4
 Texture foregroundTexture; //input image lftTop2
 Texture foregroundTexture2; //input image lftTop3
 Texture frameTexture; //input image lftTop1
  
 int healthWidth = 52;
 int healthHeight = 234;
  
 int healthMarginLeft = 41;
 int healthMarginTop = -76;
  
  int manaWidth = 81;
 int manaHeight = 160;
  
 int manaMarginLeft = 25;
 int manaMarginTop = -39;
  
 int frameWidth = 195;
 int frameHeight = 195;
  
 int frameMarginLeft = -14;
 int frameMarginTop = -39;
  
 void  OnGUI (){
  
     GUI.DrawTexture( new Rect(frameMarginLeft,frameMarginTop, frameMarginLeft + frameWidth, frameMarginTop + frameHeight), backgroundTexture, ScaleMode.ScaleToFit, true, 0 );
  
     GUI.DrawTexture( new Rect(healthMarginLeft,healthMarginTop,healthWidth + healthMarginLeft, healthHeight), foregroundTexture, ScaleMode.ScaleAndCrop, true, 0 );
  
  GUI.DrawTexture( new Rect(manaMarginLeft,manaMarginTop,manaWidth + manaMarginLeft, manaHeight), foregroundTexture2, ScaleMode.ScaleAndCrop, true, 0 );
  
     GUI.DrawTexture( new Rect(frameMarginLeft,frameMarginTop, frameMarginLeft + frameWidth,frameMarginTop + frameHeight), frameTexture, ScaleMode.ScaleToFit, true, 0 );
  
 }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Dec 11, 2013 at 02:46 AM

You don't say how "it won't work." The only thing I see wrong is that instance variables are public by default and private by default in C#. So in theory you should have 'public' in front of all of your variables. In practice, I believe all the 'int' variables should be private, so the only change is:

 public Texture backgroundTexture; //input image lftTop4
 public Texture foregroundTexture; //input image lftTop2
 public Texture foregroundTexture2; //input image lftTop3
 public Texture frameTexture; //input image lftTop1

I assume these get initialized by drag and drop in the Inspector.

Comment
Add comment · Show 4 · 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 mvp_mike13 · Dec 11, 2013 at 03:13 AM 0
Share

Now the error i'm getting is:

Assets/FinalGUIScript.cs(5,30): error CS1519: Unexpected symbol :' in class, struct, or interface member declaration Assets/FinalGUIScript.cs(5,39): error CS1519: Unexpected symbol ;' in class, struct, or interface member declaration

Assets/FinalGUIScript.cs(6,30): error CS1519: Unexpected symbol :' in class, struct, or interface member declaration Assets/FinalGUIScript.cs(6,39): error CS1519: Unexpected symbol ;' in class, struct, or interface member declaration

Assets/FinalGUIScript.cs(7,31): error CS1519: Unexpected symbol :' in class, struct, or interface member declaration Assets/FinalGUIScript.cs(7,40): error CS1519: Unexpected symbol ;' in class, struct, or interface member declaration

Assets/FinalGUIScript.cs(8,25): error CS1519: Unexpected symbol :' in class, struct, or interface member declaration Assets/FinalGUIScript.cs(8,34): error CS1519: Unexpected symbol ;' in class, struct, or interface member declaration

avatar image mvp_mike13 · Dec 11, 2013 at 03:31 AM 0
Share

Now I'm getting :

Assets/FinalGUIScript.cs(6,8): error CS0825: The contextual keyword `var' may only appear within a local variable declaration

avatar image robertbu mvp_mike13 · Dec 11, 2013 at 03:32 AM 0
Share

I'm sorry. I copied and pasted from the wrong section when I added 'public'. See corrected code. It is just your code with the word 'public' in front of all the textures.

avatar image mvp_mike13 · Dec 11, 2013 at 04:07 AM 0
Share

awesome thanks!!!

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

16 People are following this question.

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

Related Questions

How to use Farseer Physics (C# Package) in Javascript? 1 Answer

Astarpathfinding Scan all graphs 1 Answer

Having a problem translating part of a script which uses C# delegates into JS 0 Answers

Where too learn unityscript? 3 Answers

Smart Localization to JavaScript 0 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