Help converting a few lines of JS to C#.
I need help with this code. everything here seems to be NOT throwing errors, the one that shows the positioning of the "levelBar" is (the really long line) throws the error: "Assets/SpaceOverload/Scripts/experiencemanager.cs(18,49): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected"
here is the code before and after: (im 13, and just started learning C# about a month ago, so if you explain it, please explain it in a way i can understand)
JAVASCRPIT
#pragma strict
//here are public variables that are accessed in the inspector. this script manages the experience bar and the level text at the top of the screen.
var levelText:GUIText;
var levelBar:GUITexture;
var bar1:GUITexture;
var lvlUpSound:AudioClip;
//these keep track of amount of experience and level number
var expAmount:float = 0.0;
private var level:int = 1;
function Update () {
//make sure the blue part of the level bar stays the same height as the white and black bar while changing resolution
levelBar.transform.localScale = Vector3(0.24*(expAmount/(45*level)),bar1.transform.localScale.y,1);
//this checks to see if theres enough experience to level up. for this simple example we made it take 45 orbs more for every level to level up.
if(expAmount >= (45*level)){
//play the level up sound if we level up.
GetComponent.<AudioSource>().PlayOneShot(lvlUpSound);
//exp goes back to 0 so the bar is blank again.
expAmount = 0;
// add a level
level += 1;
// update the level text at the top to show the new level
levelText.text = "Lvl " + level.ToString();
//this message is received by weapons so they can keep track of the level and get better as well.
SendMessage("levelup", level, SendMessageOptions.DontRequireReceiver);
}
}
//if an orb hits the player, we gain experience. (expAmount)
function OnTriggerEnter (other : Collider){
if(other.name == "exp(Clone)"){
expAmount += 1;
Destroy(other.gameObject);
}
if(other.tag == "XPMultiplier"){
expAmount *= 2;
}
}
WHAT I TURNED INTO C#. LINE 18 throws errors
using UnityEngine;
using System.Collections;
public class experiencemanager : MonoBehaviour {
public GUIText levelText;
public GUITexture levelBar;
public GUITexture bar1;
public AudioClip lvlUpSound;
public float expAmount = 0.0f;
public int level = 0;
private AudioSource source;
// Update is called once per frame
void Update () {
levelBar.transform.localScale = Vector3 (0.24 * (expAmount / (45 * level)), bar1.transform.localScale.y, 1);
if (expAmount >= (45 * level)) {
source = GetComponent<AudioSource>();
expAmount = 0;
level += 1;
levelText.text = "Level " + level.ToString();
SendMessage("levelup", level, SendMessageOptions.DontRequireReceiver);
source.PlayOneShot(lvlUpSound);
}
}
void OnTriggerEnter (Collider other) {
if (other.name == "exp(clone)") {
expAmount += 1;
GameObject.Destroy(this);
}
if (other.tag == "XPMultiplier") {
expAmount *= 2;
}
}
}
Answer by Scribe · Aug 31, 2015 at 05:27 PM
you need the 'new' keyword (and also an f after a floating point number to notify the compiler that it is a float):
new Vector3 (0.24f * (expAmount / (45 * level)), bar1.transform.localScale.y, 1);
@scribe haha totally forgot the 'f' XD thanks and thanks for the immidiate help :D
Your answer
Follow this Question
Related Questions
Any way to use UI on a screen renderer? 0 Answers
addComponent c# script by java 0 Answers
Jump Issue :( 1 Answer
SHA512 JS -> C# 0 Answers