- Home /
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 );
}
}
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.
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
Now I'm getting :
Assets/FinalGUIScript.cs(6,8): error CS0825: The contextual keyword `var' may only appear within a local variable declaration
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.